Using Float Gives "Call to Overloaded Function Is Ambiguous" Error

Using float gives call to overloaded function is ambiguous error

5.5 is a double, but none of your functions take a double argument. So, the compiler gets confused on whether to call the function with the int parameter, or the function with the float parameter. So, you get a an error saying it is ambiguous.

That is why when you changed the function to have a double parameter, the error no longer came, because now there is a function which can take a double argument, and thus there is ambiguity there.

You can also fix the problem by calling the function as

obj.add(5.5f);

Adding the f after a number makes it to a float.

Let's look at the C++ Standard

§ 2.13.4

1 A floating literal consists of an integer part, a decimal point, a
fraction part, an e or E, an optionally signed integer exponent, and
an optional type suffix. The integer and fraction parts both consist
of a sequence of decimal (base ten) digits. Optional separating single
quotes in a digit-sequence are ignored when determining its value. [
Example: The literals 1.602’176’565e-19 and 1.602176565e-19 have the
same value. —end example ] Either the integer part or the fraction
part (not both) can be omitted; either the decimal point or the letter
e (or E ) and the exponent (not both) can be omitted. The integer
part, the optional decimal point and the optional fraction part form
the significant part of the floating literal. The exponent, if
present, indicates the power of 10 by which the significant part is to
be scaled. If the scaled value is in the range of representable values
for its type, the result is the scaled value if representable, else
the larger or smaller representable value nearest the scaled value,
chosen in an implementation-defined manner. The type of a floating
literal is double unless explicitly specified by a suffix. The
suffixes f and F specify float, the suffixes l and L specify long
double.
If the scaled value is not in the range of representable
values for its type, the program is ill-formed.

( Sorry for posting all of it, but you can learn more about floats this way )

call of overloaded function is ambiguous, double vs float

In C++ the type of decimal literals like 1.11 is defined to be double. Given that it has to convert the double to either int or float which results in the ambiguity.

A literal with an f suffix like 1.11f would have type float.

Strange ambiguous call to overloaded function error

Look at the error message from gcc:

a.cpp:16: error: call of overloaded ‘function(double, double)’ is ambiguous
a.cpp:3: note: candidates are: void function(int, int)
a.cpp:9: note: void function(float, float)

A call to either function would require truncation, which is why neither is preferred over the other. I suspect you really want void function(double y,double w). Remember that in C/C++, the default floating-point type for literals and parameter passing is double, NOT float.

UPDATE

If you really don't want to change the function signature from float to double, you can always use literals that are typed as float. If you add the suffix f to the floating point numbers, they will be typed float.

Your examples would then be function(1.2f, 2f) and function(1, 2.2f).

Call of overloaded function is ambiguous even with different argument order

You give two integers, so the compiler have to convert one into a float, but which function shall be taken?

int main()
{
fun(20.0,20);
fun( 20, 20.0);
}

these calls makes the compiler happy, since you tell which function shall be taken.

Why does this program in c++, visual studio code shows error

Type area(5.5f) to force the value to be a float.

The compiler does not know if it should cast your double value to an int or a float. Therefore, it is ambiguous.

Error: ambiguous call to overloaded function

You can press F12 on that function.

Update

Based on comments from the OP, the problem was due to a definition of acos being brought in from G3D::. Using std::acos as opposed to acos will remove the ambiguity.

Calling overloaded function with floating point literal yields 'ambiguos' error

Try a different exercise to understand this. Removing either of the two overloads will make the program compile, although there's no identity match for the literal 5.5, a double value, it can be implicitly converted to int or float.

When both overloads are present, since 5.5 can be implicitly converted to either a int or float, both are viable. The compiler is unable to decide between the two, hence the error.

Upon making the literal a float, 5.5f, we've an identity match, the float overload and no ambiguity in decision for the compiler. Conversely, keeping the literal double, 5.5, changing the function prototype from float to double also works since this is an identity match as well.

Ambiguity error using overload in c++

Because 7.5 is a double (see floating point literal), not a float; and implicit conversion to int or float are considered as the same ranking.

If your suppose 7.5 as float here you could use the suffix f or F to make it a float literal. e.g.

float fresult = calculate(7.5f, 7.5f); // 7.5f is a float literal; no ambiguity

Or use explicit conversion:

float fresult = calculate(static_cast<float>(7.5), static_cast<float>(7.5));

Ambiguous call to overloaded function in Base class

The value 9.9 can be converted to either integer, or float as your interface. Hence it is finding ambiguity that which function to invoke:

You can mention explicit conversion like :

obj1.func1((float)9.9);   

or

obj1.func1((int)9.9)

Consider the below simple test code:

#include <iostream>

using namespace std;


void test(int a)
{
cout <<" integer "<<a<<endl;
};


void test(float a)
{
cout <<"Float "<<a<<endl;
}

int main ()
{
test(10.3);
}

Try Commenting any one of the function above, it will work perfectly, where as, if you introduce both the functions, as in your code, you see ambiguity.

Hope this helps a bit ;).

error when trying to run an overloaded function with float parameters.

The type of the literal 3.5 is double, not float.

Choosing either of the overloads would require a conversion. Hence the ambiguity.

You can use 3.5f to make it a float literal.

cout << absolute(3.5f);

A better solution, IMO, would be to use a function template.

template <typename T>
T absolute(T x)
{
return (x < 0 ? -x : x);
}


Related Topics



Leave a reply



Submit