C语言函数模板问题?

#include "stdafx.h"
#include <iostream>
template <class T>
void swap(T &a,T &b);
int main(){
using namespace std;
int i=20;
int j=30;
cout<<"i,j="<<i<<j<<endl;
swap(i,j);
cout<<i<<","<<j<<endl;
getchar();
return 0;
}
void swap(T &a,T &b){
int temp;
temp=a;
a=b;
b=temp;
}

为什么报错呢?

慕姐4208626
浏览 958回答 3
3回答

富国沪深

#include <iostream>using namespace std; //引入std命名空间template <class T>void myswap(T &a,T &b); //swap在iostream定义过了,换个名字int main(){using namespace std;int i=20;int j=30;cout<<"i,j="<<i<<j<<endl;myswap(i,j);cout<<i<<","<<j<<endl;getchar();return 0;}template<class T> //这个还是要写一遍void myswap(T &a,T &b){T temp; //temp是T类型temp=a;a=b;b=temp;}

ABOUTYOU

#include "stdafx.h"#include <iostream>template <class T>void myswap(T &a,T &b); //因为标准模板库中有swap函数,系统不能识别,我只要函数名改一下就可以了int main(){using namespace std;int i=20;int j=30;cout<<"i,j="<<i<<j<<endl;swap(i,j);cout<<i<<","<<j<<endl;getchar();return 0;}template <class T> //这里也需要void myswap(T &a,T &b){T temp;temp=a;a=b;b=temp;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Maya