问题
One of my friends is trying to overload an equality operator for comparing colours in Allegro, however it does not work,He gets the error "no match for operator==" This is overloaded outside the Color class/struct, the overloaded operator function is shown below:
typedef ALLEGRO_COLOR Color;
bool operator==(const Color& rhs) const
{
if(_col.a==rhs.a && _col.b==rhs.b && _col.g==rhs.g && _col.r==rhs.r)
return true;
else
return false;
}
.
.
.
//Data member
Color _col
Im thinking this does not work because the operator is defined & implemented outside the ALLEGRO_COLOR
in Allegro,right? How can this problem be solved, is it possible to overload outside the Allegro Color struct.
回答1:
operator==
is a binary operator, but you only have one parameter. Try this:
bool operator==(const Color& _col, const Color& rhs) { ... }
Postscript: code of this form:
if ( condition )
return true;
else
return false;
is needlessly verbose in C++. Better to do this:
return condition;
In your case, I'd prefer to see:
return _col.a==rhs.a && _col.b==rhs.b && _col.g==rhs.g && _col.r==rhs.r;
来源:https://stackoverflow.com/questions/7406284/equality-operator-overloading-in-allegro