C++ ambiguous overload for ‘operator ’

前端 未结 3 622
你的背包
你的背包 2021-02-14 08:38

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

3条回答
  •  迷失自我
    2021-02-14 09:32

    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.

提交回复
热议问题