Operators overloaded within class declaration:
class Asdf{
operator float() const;
Asdf operator+(const Asdf&) const;
Asdf operator+(float);
}
int main()
{
Asdf object1, object2, object3;
//Receiving error: "more than one operator '+' matches these operands"
object1= object2 + object3;
_getch();
return 0;
}
Errors:
:error C2666: 'Asdf::operator +' : 3 overloads have similar conversions :could be 'Asdf Asdf::operator +(float)' :'Asdf Asdf::operator +(const Asdf &) const'
When I remove all conversion used with the overloaded float conversion operator the code compiles properly.
Implicit conversion operators tend to invite these kinds of ambiguities, especially when combined with implicit constructors.
From C++ Coding Standards:
A lot of this goes into efficiency and unexpected behaviors that can result from providing implicit conversions, but ambiguity errors from function overloading are included in the sort of side effects you can easily encounter when providing implicit conversion operators and/or constructors.
Solution: make your operator explicit or try to avoid providing it at all. It may be convenient, but it can invite nasty surprises like this (the compiler error is actually the least nasty).