Method overloading function call [duplicate]

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);
}

Read that error message again. Notice how it says double as the argument type it want to use.

That's because floating point constants like 3.5 are of type double. And the compiler don't know if it should convert the double value to an int or a float, thereby giving you the error.

If you want to call the float overload, use 3.5f to make it a float value. Or change your overload to use type double instead of float.


you write that 3.5 is a float this value is not a float it is a double.