3

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?

Shahriar
  • 13,460
  • 8
  • 78
  • 95
  • 1
    Which conversion do you want ? double -> int or double -> float ? – Jarod42 Dec 10 '15 at 17:46
  • 1
    It just so happens that C++ specifies `double->int` to be equally valid to `double->float`. One of those could have been made better than the other, but that wasn't the decision made. – chris Dec 10 '15 at 17:47
  • 1
    Try this: `print(1.2f);` – zdf Dec 10 '15 at 17:47

3 Answers3

5

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.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
1

It is interpreting 1.2 as a double. Casting it to a float will solve the problem.

print( float(1.2) );

Nick
  • 11
  • 2
1

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.

Mureinik
  • 297,002
  • 52
  • 306
  • 350