Trying to understand operator overloading float ambiguous error

Hello I am trying to understand operator overloading and have written my own example.
When I try to compile it says the float is ambiguous. What does that mean I cant find anything on google. Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include<iostream>
using namespace std;


class doSum
	{
		public:
	int sum (int a, int b)
	{ int c = a+b;
	return c;
	
	cout << "The answer is " << c << "\n";}
	
	float sum (float a ,float b)
	{
		float c;
		c = a+b;
		return c;
		cout << "The answer is " << c << "\n";}
	
	char sum (char a, char b)
	{
		char c = a + b;
		return c;
		cout << "The answer is " << c << "\n";
	}
	};

int main()
{
	
	
	
 doSum ints;
 ints.sum(7,8);
 doSum floats;
 floats.sum (6.7,7.8);
 doSum chars;
 chars.sum(64,99);

 
	}
 
	
	
When you call ints.sum(7,8) the compiler can't tell if you want to call the float version or the char version, since 7 and 8 are freely convertible to either type.
I would think ints.sum(7,8) is ok because int is overloaded.

The compiler thinks you are passing 2 doubles when you do this floats.sum (6.7,7.8) and so it can't decide between int and float. Change it to floats.sum (6.7f,7.8f);
https://stackoverflow.com/questions/30003519/using-float-gives-call-to-overloaded-function-is-ambiguous-error

You are never calling the char version (64 and 99 are ints first).

You can't put cout after the return statements, they will never be called.
Topic archived. No new replies allowed.