c++报错:[Error] call of overloaded 'swap(int&, int&)' is ambiguous

错误提示:

[Error] call of overloaded 'swap(int&, int&)' is ambiguous

错误代码:

#include <iostream>
using namespace std;
template
<typename T> void swap(T &x,T &y){ T z; z=x;x=y;y=z; } int main() { int a=1,b=2; swap(a,b); //这里报错 cout<<a <<b<<"\n"; double c=1.1,d=2.2; swap(c,d); cout<<c <<d<<"\n"; return 0; }

原因:

swap函数和库中swap函数重名了。

解决方法:

1.修改函数名

2.在swap前面加::

 

posted @ 2022-11-20 16:17  Mr_宋先生  阅读(751)  评论(0编辑  收藏  举报