C++ 模版调用不明确错误
谁能告诉我错误的原因吗 错误是C++ 模版调用不明确错误,c++,C++,谁能告诉我错误的原因吗 错误是 C:\web\template1.cpp||In function 'int main()':| C:\web\template1.cpp|23|error: call of overloaded 'swap(int&, int&)' is ambiguous| C:\web\template1.cpp|6|note: candidates are: void swap(X&, X&) [with X = int]| c:\prog
C:\web\template1.cpp||In function 'int main()':|
C:\web\template1.cpp|23|error: call of overloaded 'swap(int&, int&)' is ambiguous|
C:\web\template1.cpp|6|note: candidates are: void swap(X&, X&) [with X = int]|
c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\move.h|76|note: void std::swap(_Tp&, _Tp&) [with _Tp = int]|
C:\web\template1.cpp|24|error: call of overloaded 'swap(double&, double&)' is ambiguous|
C:\web\template1.cpp|6|note: candidates are: void swap(X&, X&) [with X = double]|
c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\move.h|76|note: void std::swap(_Tp&, _Tp&) [with _Tp = double]|
C:\web\template1.cpp|25|error: call of overloaded 'swap(char&, char&)' is ambiguous|
C:\web\template1.cpp|6|note: candidates are: void swap(X&, X&) [with X = char]|
c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\move.h|76|note: void std::swap(_Tp&, _Tp&) [with _Tp = char]|
||=== Build finished: 3 errors, 0 warnings ===|
#包括
使用名称空间std;
模板
无效掉期(X&a、X&b)
{
X温度=a;
a=b;
b=温度;
}
int main()
{
int i=10,j=20;
双x=10.1,y=23.3;
字符a='x',b='z';
cout您的代码中已经声明了一个交换函数。请更改该函数的名称或删除另一个交换函数的声明。您的swap
与std::swap
冲突。请使用上述命名空间std;
删除,并更正std
命名空间中的其余代码
std::cout<<"i="<<i<<"\tj="<<j<<std::endl;
std::cout<<"x="<<x<<"\ty="<<y<<std::endl;
std::cout<<"a="<<a<<"\tb="<<b<<std::endl;
#include <iostream>
using namespace std;
template <typename X>
void Swap(X &a, X &b)
{
X temp = a;
a = b;
b = temp;
}
int main()
{
int i=10, j=20;
double x=10.1, y=23.3;
char a='x', b='z';
cout<<"i="<<i<<"\tj="<<j<<endl;
cout<<"x="<<x<<"\ty="<<y<<endl;
cout<<"a="<<a<<"\tb="<<b<<endl;
Swap(i,j);
Swap(x,y);
Swap(a,b);
cout<<"i="<<i<<"\tj="<<j<<endl;
cout<<"x="<<x<<"\ty="<<y<<endl;
cout<<"a="<<a<<"\tb="<<b<<endl;
return 0;
}
std::cout将swap()
重命名为swap2()
或类似名称。或者,最好不要使用命名空间std;
发出
如前所述,您的代码通过iostream引入std::swap()
,然后通过使用名称空间将其转储到您自己的swap()
上。
有两种方法可以克服这个问题
1) 不要使用命名空间std声明;
使用std::cout
每当您需要为新行打印任何和std::endl
#include <iostream>
template <typename X>
void swap(X &a, X &b)
{
X temp = a;
a = b;
b = temp;
}
int main()
{
int i=10, j=20;
double x=10.1, y=23.3;
char a='x', b='z';
std::cout<<"i="<<i<<"\tj="<<j<<std::endl;
std::cout<<"x="<<x<<"\ty="<<y<<std::endl;
std::cout<<"a="<<a<<"\tb="<<b<<std::endl;
swap(i,j);
swap(x,y);
swap(a,b);
std::cout<<"i="<<i<<"\tj="<<j<<std::endl;
std::cout<<"x="<<x<<"\ty="<<y<<std::endl;
std::cout<<"a="<<a<<"\tb="<<b<<std::endl;
return 0;
}
所有这些建议都是在七年多前提出的。你的回答补充了什么?什么是“一般功能”?
#include <iostream>
using namespace std;
template <typename X>
void Swap(X &a, X &b)
{
X temp = a;
a = b;
b = temp;
}
int main()
{
int i=10, j=20;
double x=10.1, y=23.3;
char a='x', b='z';
cout<<"i="<<i<<"\tj="<<j<<endl;
cout<<"x="<<x<<"\ty="<<y<<endl;
cout<<"a="<<a<<"\tb="<<b<<endl;
Swap(i,j);
Swap(x,y);
Swap(a,b);
cout<<"i="<<i<<"\tj="<<j<<endl;
cout<<"x="<<x<<"\ty="<<y<<endl;
cout<<"a="<<a<<"\tb="<<b<<endl;
return 0;
}