|
想问一下,GCC4.0是不是不支持模板?代码和错误提示如下:
//funtemp.cpp --using a function template
#include<iostream>
template <class T>
void swap(T &a,T &b);
int main()
{
using namespace std;
int i=10,j=20;
cout<<"i,j="<<i<<","<<j<<".\n";
cout<<"Using compiler-generated int swapper: \n";
swap(i,j);
cout<<"Now i,j="<<i<<","<<j<<".\n";
double x=24.5,y=81.7;
cout<<"x,y="<<x<<","<<y<<".\n";
cout<<"Using comiler-generated double swapper: \n";
swap(x,y);
cout<<"Now x,y="<<x<<","<<y<<".\n";
cin.get();
return 0;
}
template <class T>
void swap(T &a,T &b)
{
T temp;
temp=a;
a=b;
b=temp;
}
funtemp.cpp: In function ‘int main()’:
funtemp.cpp:13: 错误:调用重载的 ‘swap(int&, int&)’ 有歧义
funtemp.cpp:5: 附注:备选为: void swap(T&, T&) [with T = int]
/usr/lib/gcc/i386-redhat-linux/4.1.0/../../../../include/c++/4.1.0/bits/stl_algobase.h:92:
附注: void std::swap(_Tp&, _Tp&) [with _Tp = int]
funtemp.cpp:19: 错误:调用重载的 ‘swap(double&, double&)’ 有歧义
funtemp.cpp:5: 附注:备选为: void swap(T&, T&) [with T = double]
/usr/lib/gcc/i386-redhat-linux/4.1.0/../../../../include/c++/4.1.0/bits/stl_algobase.h:92:
附注: void std::swap(_Tp&, _Tp&) [with _Tp = double] |
|