suh*_*has 3 c++ templates swap
在这个程序中调用swap函数给出一个叫做overloded函数调用的错误是ambiguos.please告诉我如何解决这个问题.是否有任何不同的方法来调用模板函数
#include<iostream>
using namespace std;
template <class T>
void swap(T&x,T&y)
{
T temp;
temp=x;
x=y;
y=temp;
}
int main()
{
float f1,f2;
cout<<"enter twp float numbers: ";
cout<<"Float 1: ";
cin>>f1;
cout<<"Float 2: ";
cin>>f2;
swap(f1,f2);
cout<<"After swap: float 1: "<<f1<<" float 2:"<<f2;
int a,b;
cout<<"enter twp integer numbers: ";
cout<<"int 1: ";
cin>>a;
cout<<"int 2: ";
cin>>b;
swap(a,b);
cout<<"After swap: int 1: "<<a<<" int 2:"<<b;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
您的函数与定义的函数冲突,其中move.h包含一些隐式包含的函数.如果删除,using namespace std则应修复此问题 - 您在std命名空间中定义了与之冲突的函数.