《c++primer plus》8.9例题
改动一点,不能编译?
//funtmp.cc
#include<iostream>
using namespace std;
template<class any>
void swap(any &a,any &b);
int main()
{
int a;
a=4;
int b;
b=5;
double c=3.3;
double d=4.4;
swap(a,b);
swap(c,d);
cout<<"a b"<<a<<" "<<b<<endl;
cout<<"c d"<<c<<" "<<d<<endl;
return 0;
}
template<class any>
void swap(any &a,any &b)
{
any tmp;
tmp=a;
a=b;
b=tmp;
return;//书上没有return;
}
#g++ funtmp.cc
//出错信息:
funtmp.cc: In function `int main()':
funtmp.cc:15: call of overloaded `swap(int&, int&)' is ambiguous
funtmp.cc:5: candidates are: void swap(any&, any&) [with any = int]
/usr/include/c++/3.2/bits/stl_algobase.h:121: void
std::swap(_Tp&, _Tp&) [with _Tp = int]
funtmp.cc:16: call of overloaded `swap(double&, double&)' is ambiguous
funtmp.cc:5: candidates are: void swap(any&, any&) [with any = double]
/usr/include/c++/3.2/bits/stl_algobase.h:121: void
std::swap(_Tp&, _Tp&) [with _Tp = double]
把swap定义在文件开头也一样。
改怎么搞,我在vc6.0通过。 |