I have two overloaded function like below:
void print(int i) { ... }
void print(float f) { ... }
Its giving me this error for print(1.2);
:
error: call of overloaded 'print(double)' is ambiguous
Can anyone explain me why?
I have two overloaded function like below:
void print(int i) { ... }
void print(float f) { ... }
Its giving me this error for print(1.2);
:
error: call of overloaded 'print(double)' is ambiguous
Can anyone explain me why?
1.2 is a double literal not a float.
So the compiler requires an explicit disambiguation.
1.2f would work as that is a float literal.
It is interpreting 1.2 as a double. Casting it to a float will solve the problem.
print( float(1.2) );
1.2
is a double
literal, making the function you're trying to call ambiguous - a double
can just as easily be truncated to a float
or to an int
. Using a float
literal (1.2f
) or explicitly casting it would solve the problem.