c++ - 重载 'swap(float&, float&)' 的调用不明确

标签 c++ templates swap

在这个程序中,交换函数的调用给出了一个称为调用覆盖函数的错误是模棱两可的。请告诉我如何解决这个问题。有没有调用模板函数的不同方法

     #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 命名空间中定义的。

关于c++ - 重载 'swap(float&, float&)' 的调用不明确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19789414/

相关文章:

c++ - 如何找到包含 QtVirtualKeyboard 的窗口

c++ - 在 C++ 中使用模板的 2s 幂数组

templates - GRAILS:我可以从 _FORM.GSP 模板调用服务逻辑吗?

c++ - 通过检测图像中的特定大对象或 Blob 来裁剪图像?

c++ - 如何在 OpenCL 内核中使用 C++ 模板?

c++ - 为 iPhone 应用程序解析 RSS/Atom 提要的最佳方法是什么?

templates - symfony - 从 View 中调用一个 Action ?

java - 将数组中的每个元素与其相邻元素交换

c# - 输出不是在同一数组中交换元素的预期输出

C++ vector : should I copy or swap?