重载的"swap(float&, float&)"的调用是不明确的
call of overloaded 'swap(float&, float&)' is ambiguous
在这个程序中,调用交换函数给出一个错误,称为调用overloded function是模棱两可的。请告诉我如何解决这个问题。 是否有任何不同的方法来调用模板函数
#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;
}
您的函数与某些包含隐式包含的 move.h
中定义的函数冲突。如果删除using namespace std
则应解决此问题 - 与您冲突的函数是在 std
命名空间中定义的。
通过将交换
函数更改为my_swap函数,它解决了问题。 因为 swap 也是 C++ 中的预定义函数
#include<iostream>
using namespace std;
template <class T>
void my_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;
my_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;
my_swap(a,b);
cout<<"After swap: int 1: "<<a<<" int 2:"<<b;
return 0;
}
当然,重命名您的函数或删除 ->库中已经有一个:
http://www.cplusplus.com/reference/algorithm/swap/
这就是您的编译器所抱怨的。
相关文章:
- 什么时候调用组成单元对象的析构函数
- 对RValue对象调用的LValue ref限定成员函数
- 为什么使用 "this" 指针调用派生成员函数?
- 函数调用中参数的顺序重要吗
- OpenGL - 在抛出"__gnu_cxx::recursive_init_error"实例后终止调用?
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 在c++类上调用void函数
- 为什么 std::unique 不调用 std::sort?
- 调用专用模板时出错"no matching function for call to [...]"
- 选择要调用的构造函数
- C++为什么尽管我调用了void函数,它却不起作用
- 构造函数正在调用一个使用当前类类型的函数
- 变量没有改变?通过向量的函数调用
- 没有为自己的结构调用列表推回方法
- 如何使用ABI调用过程在堆栈上正确传递Float参数
- 没有匹配函数来调用 std::vector<float>
- 重载的"swap(float&, float&)"的调用是不明确的
- Clang模糊调用(int、float、double)
- 调用"事件:<float>:通知(NULL, const double&)"没有匹配函数|