I\'v read several posts here about this kind of errors, but I wasn\'t able to solve this one... Has soon I define the operator int and the function f, fails to compile. I teste
The compiler sees two ways to interpret a + 4
. It can convert the 4
to an object of type Fraccao
and use operator+(const Fraccao&, const Fraccao&)
or it can convert a
to type int
with the member conversion operator, and add 4
to the result. The rules for overloading make this ambiguous, and that's what the compiler is complaining about. In general, as @gx_ said in a comment, this problem comes up because there are conversions in both directions. If you mark the operator int()
with explicit
(C++11) the code will be okay.