-2

I am new to C++ so please bear me and see my problem and help me to solve my problem.

Here I just tried to know which function will execute if there is a more than one function with same name But I left with an Error

error: call of overloaded ‘sum(int, int)’ is ambiguous

MY CODE

#include<iostream>
using namespace std;

// A function with default arguments, it can be called with
//2 arguments or 3 arguments or 4 arguments.
int sum(int x, int y, int z=0, int w=0)
{
    return (x + y + z + w);
}
int sum(int x, int y, float z=0, float w=0)
{
return (x + y + z + w);
}
/* Driver program to test above function*/
int main()
{
    cout << sum(10, 15) << endl;
    cout << sum(10, 15, 25) << endl;
    cout << sum(10, 15, 25, 30) << endl;
    return 0;
}

Help what is happening here and what is the error I am getting an error

  • 7
    To help answer the question, you should share which of the two `sum` function you expect `sum(10, 15)` to call, and why you expect that. – François Andrieux Nov 16 '21 at 14:50
  • 6
    The compiler doesn't know which of the two functions should be called either, that's why you get that error. – Jesper Nov 16 '21 at 14:52
  • The problem comes from the parameter default values... You could make ALL parameters to the second overload `float`s to make it work. – Michaël Roy Nov 16 '21 at 15:02
  • 2
    Because of the default arguments, `sum(10, 15)` could call the first version as `sum(10, 15, 0, 0)` or it could call the second version as `sum(10, 15, 0.0f, 0.0f)`. Neither one is better than the other, so the call is ambiguous. – Pete Becker Nov 16 '21 at 15:12
  • @PeteBecker Ya but if I remove the line cout << sum(10,15) << endl; The code works good with rest of the two lines sum(10,15,25) ,sum(10,15,25,30) but why?? – Vijayakash Allenki Nov 16 '21 at 15:18
  • 1
    If you remove `cout << sum(10,15) << endl` then you remove the problem because there is nothing ambiguous about calling the other two couts – drescherjm Nov 16 '21 at 15:18
  • 1
    The other two couts work because you passed at least 3 `int`s so it knows to call the `int sum(int x, int y, int z=0, int w=0)` there is no reason to prefer the `int sum(int x, int y, float z=0, float w=0)` over the `int sum(int x, int y, int z=0, int w=0)` one. – drescherjm Nov 16 '21 at 15:21

1 Answers1

2

Your ambiguity is on line

cout << sum(10, 15) << endl;

This is because compiler doesn't know if third argument is float or int

In this line:

cout << sum(10, 15, 25) << endl;

you are calling to:

int sum(int x, int y, int z=0, int w=0)

because your third argument is int, not float... if you had called it using

cout << sum(10, 15, 25.0f) << endl;

Compiler would have noticed that third argument was float and

int sum(int x, int y, float z=0, float w=0)

will be called.

You are always using ints on your code. floats have to be written with dots. (0 is int, 0.0 is double, 0.0f is float)

int sum(int x, int y, float z=0, float w=0)

should be

int sum(int x, int y, float z=0.0f, float w=0.0f)

Edit: I've changed my answer following @Pete Becker comment. Appending f suffix to float constants (0.0f)

Ref:https://en.cppreference.com/w/cpp/language/floating_literal

  • In `float z=0.0` the 0.0 is a double. `float z=0.0f` would be a float. Related: [https://stackoverflow.com/questions/5199338/what-is-the-significance-of-0-0f-when-initializing-in-c/5199515](https://stackoverflow.com/questions/5199338/what-is-the-significance-of-0-0f-when-initializing-in-c/5199515) – drescherjm Nov 16 '21 at 16:09
  • `float z = 0` is okay; there is an implicit conversion from `int` to `float`. Some people prefer using more closely-typed initializers, so for those folks, `float z = 0.0` or `float z = 0.0f` is preferable. Personally, I go back and forth on this one. +1. – Pete Becker Nov 16 '21 at 16:16