对函数的引用不明确 [英] Reference to function is ambiguous

查看:185
本文介绍了对函数的引用不明确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么会导致编译器错误,说明我的引用不明确?我有一个 float int string 所有创建单独的函数签名,对吗?



这里是我到目前为止:

 code> #include< iostream> 
#include< string>
using namespace std;

int plus(int a,int b);
float plus(float a,float b);
string plus(string a,string b);

int main(void)
{
int n = plus(3,4);
double d = plus(3.2,4.2);
string s = plus(he,llo);
string s1 =aaa;
string s2 =bbb;
string s3 = plus(s1,s2);
}

int plus(int a,int b){
return a + b;
} // int version

float plus(float a,float b){
return a + b;
} // float version

string plus(string a,string b){
return a + b;
} //字符串版本


解决方案

请勿使用 using namespace std; 。在这种情况下,恰好有一个名为 std :: plus -oh,wait,never mind,that's actually called plus 并且它的构造函数被抛出到桶中用于重载解决你的函数加。






其次,你有一个歧义,因为 3.2 和 4.2 的类型为 double ,并且可以很好地转换为 float int



这是一个简化,它涉及到将数字传递给重载函数,C ++基本上使用这些规则:


  1. 如果所有的参数完​​全匹配一个重载,

  2. 如果所有参数都可以提升为匹配其中一个重载,那么使用那个。


  3. 如果您在特定级别有多个候选人,那么您可以使用一个是一个模糊。至关重要的是, double 不会升级到 float - 这将是降级。因此,它必须使用标准转换为 float ,这与标准转换结合到 int ,因此两个重载是不明确的。


    Why is this cause a compiler error, stating that my references are ambiguous? I have a float, int and string, which should all create separate function signatures, right?

    Here is what I have so far:

    #include <iostream>
    #include <string>
    using namespace std;
    
    int plus(int a, int b);
    float plus(float a, float b);
    string plus(string a, string b);
    
    int main(void)
    {
        int n = plus(3, 4);
        double d = plus(3.2, 4.2);
        string s = plus("he", "llo");
        string s1 = "aaa";
        string s2 = "bbb";
        string s3 = plus(s1, s2);
    }
    
    int plus(int a, int b) {
        return a+b;
    } // int version
    
    float plus(float a, float b) {
        return a+b;
    } // float version
    
    string plus(string a, string b) {
        return a+b;
    } // string version
    

    解决方案

    First, don't use using namespace std;. In this case, there happens to be a struct called std::plus—oh, wait, never mind, that's actually called plus and its constructor is thrown into the bucket for overload resolution with your function called plus.


    Second, you have an ambiguity because 3.2 and 4.2 are of type double and can convert equally well to float or int.

    This is a simplification, but when it comes to passing numbers to an overloaded function, C++ basically uses these rules:

    1. If all the arguments exactly match one of the overloads, then use that one.
    2. If all the arguments can be promoted to match one of the overloads, then use that one.
    3. If all the arguments can be numerically converted to match one of the overloads, then use that one.

    If you have multiple candidates at a specific level, then that is an ambiguity. Crucially, a double does not promote to a float—that would be a downgrade. So it has to use a standard conversion to a float, which ties with the standard conversion to an int, so those two overloads are ambiguous.

    这篇关于对函数的引用不明确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆