David
David

Reputation: 159

Ambiguity error using overload in c++

I have tried to use overload with floating and integer. When I only used the integer, the code worked fine, but when I included the floating it gave me errors. The code is the following:

#include <iostream>
using namespace std;

int calculate(int x,int y);
float calculate(float x,float y);
const int MAININT=4;

int main()
{
    int result=calculate(5,5);
    float fresult=calculate(7.5,7.5);                 LINE X
    cout << (result + MAININT + fresult);             LINE Y
    return 0;
}

int calculate(int x,int y)
{
    int result=x*y;
    return result;
}

float calculate(float x,float y)
{
    int result=x*y;
    return result;
}

By deleting LINE X and fresult from LINE Y, the code give me no errors. So I assume there must be something wrong in LINE X, but I don't understand why I get errors.

The error messages I got was :

[Error] call of overloaded 'calculate(double, double)' is ambiguous
[Note] candidates are:
[Note] int calculate(int, int)
[Note] float calculate(float, float)

I did not understand the error messages, so I didn't include them. I understand what I did wrong from the answer of songyuanyao, but next time I will include the error messages in my question from the start so it will be easier to see what I have done wrong in the code.

Upvotes: 2

Views: 859

Answers (3)

songyuanyao
songyuanyao

Reputation: 172964

Because 7.5 is a double (see floating point literal), not a float; and implicit conversion to int or float are considered as the same ranking.

If your suppose 7.5 as float here you could use the suffix f or F to make it a float literal. e.g.

float fresult = calculate(7.5f, 7.5f); // 7.5f is a float literal; no ambiguity

Or use explicit conversion:

float fresult = calculate(static_cast<float>(7.5), static_cast<float>(7.5));

Upvotes: 10

Christian Hackl
Christian Hackl

Reputation: 27538

While others have already told you where the ambiguity error comes from, I'm surprised nobody has mentioned the easiest solution to the problem: Just use double instead of float.

Just like int should be your default choice for integer numbers, double should be your default choice for floating-point numbers. float is for very special use cases, none of which are likely to apply in your situation. See "When do you use float and when do you use double" over at Software Engineering Stack Exchange.

As a side effect of following this guideline, your particular problem here disappears:

#include <iostream>
using namespace std;

int calculate(int x,int y);
double calculate(double x, double y);
const int MAININT=4;

int main()
{
    int result=calculate(5,5);
    double fresult=calculate(7.5,7.5);
    cout << (result + MAININT + fresult);
    return 0;
}

int calculate(int x,int y)
{
    int result=x*y;
    return result;
}

double calculate(double x, double y)
{
    double result=x*y; // the `int` here was a mistake in your original code anyway
    return result;
}

Some further suggestions:

  • Avoid using namespace std;.
  • Reserve ALL_CAPS for preprocessor macros.

Upvotes: 0

Saurav Sahu
Saurav Sahu

Reputation: 13954

You should have posted the error message which is self-explanatory in itself. The error message mentions about the candidate functions and how they are not exactly compatible:

error: call of overloaded 'calculate(double, double)' is ambiguous
     float fresult=calculate(7.5,7.5);                
                                    ^
note: candidate: int calculate(int, int)
 int calculate(int x,int y);
     ^
note: candidate: float calculate(float, float)
 float calculate(float x,float y);

By default, a floating-point literal (7.5 in your case) is of type double.

Here is the list of suffix that determines the type of the floating-point literal:

  1. (no suffix) defines double
  2. f F defines float
  3. l L defines long double

Upvotes: 1

Related Questions