-2

I've been programming a pair of overloaded C++ functions, one taking 2 integer parameters, and the other 2 floats.

But the codeblocks compiler says:

error: call of overloaded 'func(double, double)' is ambiguous

Why double if I'm specifying a float?

I'm using the two functions to sum their values and showing the result on cout inside them. Float values given as arguments are 1.14 and 3.33, not big floating numbers...

Someone knows? thx!

#include <iostream>
using namespace std;

void func(int a, int b) {
    cout << a+b;
}

void func(float a, float b) 
    cout << a+b;
}

int main() {
  func(3, 7);
  func(1.14, 3.33);

}
gflegar
  • 1,583
  • 6
  • 22

1 Answers1

4

The function call func(1.14, 3.33) is ambiguous because 1.14 and 3.33 are doubles and both can be converted to either int or float. Therefore the compiler does not know which function to call.

You can fix this by explicitly specifying the type of the constants func(float(1.14), float(3.33)) or by changing the overload from func(float, float) to func(double, double).

In this case, explicitly telling the compiler which type to use is probably a better idea.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458