Equality Operator Overloading in Allegro

旧街凉风 提交于 2019-12-13 18:23:46

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!