C++模板函数问题--这段程序到底哪里错了呀?
//f1401--定义合计使用函数模板#include<iostream>usingnamespacestd;template<typenameT>voidswap(T&...
//f1401--定义合计使用函数模板
#include<iostream>
using namespace std;
template<typename T>
void swap(T& a,T& b){
T temp=a;a=b;b=temp;
}
int main(){
double dx=3.5,dy=6.5;
int ix=6,iy=7;
string s1="good",s2="better";
swap(dx,dy);
swap(ix,iy);
swap(s1,s2);
cout<<dx<<" "<<dy<<endl;
cout<<ix<<" "<<iy<<endl;
cout<<s1<<" "<<s2<<endl;
system("pause");
}
error C2667: 'swap' : none of 2 overload have a best conversion
error C2668: 'swap' : ambiguous call to overloaded function
error C2667: 'swap' : none of 2 overload have a best conversion
error C2668: 'swap' : ambiguous call to overloaded function
error C2667: 'swap' : none of 2 overload have a best conversion
error C2668: 'swap' : ambiguous call to overloaded function
error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there i
s no acceptable conversion) 展开
#include<iostream>
using namespace std;
template<typename T>
void swap(T& a,T& b){
T temp=a;a=b;b=temp;
}
int main(){
double dx=3.5,dy=6.5;
int ix=6,iy=7;
string s1="good",s2="better";
swap(dx,dy);
swap(ix,iy);
swap(s1,s2);
cout<<dx<<" "<<dy<<endl;
cout<<ix<<" "<<iy<<endl;
cout<<s1<<" "<<s2<<endl;
system("pause");
}
error C2667: 'swap' : none of 2 overload have a best conversion
error C2668: 'swap' : ambiguous call to overloaded function
error C2667: 'swap' : none of 2 overload have a best conversion
error C2668: 'swap' : ambiguous call to overloaded function
error C2667: 'swap' : none of 2 overload have a best conversion
error C2668: 'swap' : ambiguous call to overloaded function
error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there i
s no acceptable conversion) 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询