Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference to function is ambiguous [duplicate]

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
like image 366
MarJamRob Avatar asked Feb 23 '13 06:02

MarJamRob


People also ask

How to fix ambiguous call to overloaded function?

There are two ways to resolve this ambiguity: Typecast char to float. Remove either one of the ambiguity generating functions float or double and add overloaded function with an int type parameter.

How do you remove ambiguous error in C++?

You can resolve ambiguity by qualifying a member with its class name using the scope resolution ( :: ) operator. The statement dptr->j = 10 is ambiguous because the name j appears both in B1 and B2 .

What is ambiguity in function overloading?

When the compiler is unable to decide which function it should invoke first among the overloaded functions, this situation is known as function overloading ambiguity. The compiler does not run the program if it shows ambiguity error.

Where can an ambiguity occur in overloading a function?

If the compiler can not choose a function amongst two or more overloaded functions, the situation is -” Ambiguity in Function Overloading”. The reason behind the ambiguity in above code is that the floating literals 3.5 and 5.6 are actually treated as double by the compiler.


1 Answers

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.

like image 112
John Calsbeek Avatar answered Oct 20 '22 00:10

John Calsbeek