- 论坛徽章:
- 0
|
#include <iostream>
using namespace std;
template<class T>
void swap(T &a,T &b);
int main()
{
cout.precision(2);
cout.setf(ios::fixed,ios::floatfield);
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<<endl;
return 0;
}
template<class T>
void swap(T &a,T &b)
{
T temp;
temp=a;
a=b;
b=temp;
}
出错信息如下:
hello.cpp: In function `int main()':
hello.cpp:12: call of overloaded `swap(int&, int&)' is ambiguous
hello.cpp:4: candidates are: void swap(T&, T&) [with T = int]
/usr/include/c++/3.2/bits/stl_algobase.h:121: void
std::swap(_Tp&, _Tp&) [with _Tp = int] |
|