1

I wanted to know why I am getting the compilation error

call of overloaded 'my_add(double, double, double)' is ambiguous

when I am trying to overload my_add function as follows:

//=============== Start of Program ===========
#include <iostream.h>
int my_add( int x, int y , int z) 
   {
    return (x+y+z); 
   }
float my_add( float x, float y , float z) 
   {
    return (x+y+z); 
   }
int main (void)
{
 cout<<my_add(5,6,1);
 cout<<my_add(5.5,6.5,1.0);
} 

//=============== End of Program ===========

However, the code compiles fine when I change the floats to doubles in my_add( float x, float y , float z).

What I have learnt is that for overloading a function it's definitions should differ in number of parameters, order of parameters or type of parameters.

In my case, the two versions of the functions differ in type of parameters.

The IDE I am using is Code Blocks.

YSC
  • 38,212
  • 9
  • 96
  • 149
hitx11
  • 43
  • 5
  • 3
    `5.0` is a double litteral (`5.0f` is for floats). Therefore these need to be converted, and since `int` and `float` are both valid candidates the overloads are ambiguous. – Borgleader Dec 07 '16 at 13:35
  • Dear Michael, I don't get "since int and float` are both valid candidates". Can you explain in more detail ? – hitx11 Dec 07 '16 at 13:39
  • You passed an argument of type double to an overloaded function, it can choose between the one that takes ints or the one that takes floats. The problem is there is an implicit conversion from double to int and from double to float but the compiler doesn't know which one you want, so the overloads are ambiguous. (P.S: My username is not Micheal) – Borgleader Dec 07 '16 at 13:43
  • @Borgleader but isn't 5.5 a float ? Is it necessary to append the 'f' to 5.5 when we want to pass it as a float ? I always thought 5.5 was a float or a double, the only difference between the both being the range of values the two types could take. – hitx11 Dec 07 '16 at 13:54
  • As I said before, `5.5` is a double literal, its type is `double`. Normally it doesn't matter because the implicit conversion will convert the double to a float, but here because you also have an overload for int, the compiler doesn't know which conversion it should do. – Borgleader Dec 07 '16 at 14:03

1 Answers1

4

5.5 is actually a double literal. So it needs a conversion to match one of your overloads. But it can match both with a single conversion, which leads to ambiguity.

Solutions include either using a float literal (5.5f), or casting. It will suffice to apply the solutions even to a single parameter in order to resolve the ambiguity.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
  • Dear Story Teller, Can you explain " it can match both with a single conversion" ? – hitx11 Dec 07 '16 at 13:40
  • @HitX11 C++ resolves calls to overloaded functions in a process known as overload resolution. It does so by matching the types of the arguments, to the types of the functions formal parameters. Part of it involves doing implicit conversions (such as `int` to `float` or vice-versa), if there isn't an exact match. – StoryTeller - Unslander Monica Dec 07 '16 at 13:44
  • Teller, but isn't 5.5 a float ? Is it necessary to append the 'f' to 5.5 when we want to pass it as a float ? I always thought 5.5 was a float or a double, the only difference between the both being the range of values the two types could take – hitx11 Dec 07 '16 at 14:00
  • @HitX11 It's not a `float` literal, it's a `double`. Compatible they may to a point, but different types nonetheless. You can't get around it, c++ is a strongly typed language. – StoryTeller - Unslander Monica Dec 07 '16 at 14:03
  • Floating-point literals are always of type `double`. If you want them to have the type `float`, you need to use the `f` suffix. @HitX11 – Cody Gray - on strike Dec 07 '16 at 14:03
  • @Story Teller So next time I am given a number with decimal point in it, how do I say it's a double or a float ? Is the 'f' needed to differentiate? Basically I need to know what is the best method to identify whether its a float literal or double literal ? – hitx11 Dec 07 '16 at 14:22
  • 3
    @HitX11 `5.5` is `double`, `5.5f` is `float`, as explained [here](http://stackoverflow.com/questions/41018861/41018925#comment69246012_41018925), [here](http://stackoverflow.com/questions/41018861/41018925#comment69246031_41018861), [here](http://stackoverflow.com/questions/41018861/41018925#comment69244838_41018861), and [here](http://stackoverflow.com/a/41018925/1012936) – milleniumbug Dec 07 '16 at 14:42