Ambiguous overload for operator + using conversion

I have a class with conversions konw -> int, double -> konw:

class konw {
    double re, im;
public:
    konw() : re(0.0), im(0.0) {}
    konw(double r, double i = 0.0) : re(r), im(i) {}
    operator int() {return re;}
    konw operator+(konw a) {konw wynik; wynik.re = re + a.re; wynik.im = im + a.im; return wynik;}
};

I mainly test these conversions with the overloaded + operator

konw zesp(3.1, 0.6);
int ssuma = zesp + 6;

The compiler reports an error while working on the last attached line saying that:

ambiguous overload for 'operator+' in 'zesp + 6'

As far as I read, if there are several ways to call an overloaded function, the compiler chooses the shortest one. Of course, there should be one and only such way. I could find two ways to call the + operator:

  • zespkonw โ†’ int conversion variable and calloperator+(int, int)
  • conversion constant 6int-> double-> konw and callkonw::operator+(konw)

1- , 2-, Imo , . ? ?

+4
2

"" " " . ([ovr.ics.rank] ), , " " . .

+4

:

int ssuma = zesp + 6;
                   ^
prog.cpp:15:19: note: candidates are:
prog.cpp:15:19: note: operator+(int, int) <built-in>
prog.cpp:10:10: note: konw konw::operator+(konw)

:

  • ", , zesp + 6, , konw::operator+ 6 konw object"
  • "wait, zesp int operator+(int, int)"

, , :

int ssuma = int(zesp) + 6;
+2

All Articles