Allegro中的平等运算符重载 [英] Equality Operator Overloading in Allegro

查看:115
本文介绍了Allegro中的平等运算符重载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一个朋友正在尝试重载用于在Allegro中比较颜色的相等运算符,但是它不起作用.他得到错误操作符不匹配==".这是在Color类/结构之外重载的,重载了操作员功能如下所示:

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

我认为这是行不通的,因为已定义了运算符&在Allegro的ALLEGRO_COLOR外部实现,对吗?如何解决这个问题,是否有可能在Allegro Color结构之外重载.

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.

推荐答案

operator==是二进制运算符,但是只有一个参数.试试这个:

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;

在C ++中是不必要的冗长.最好这样做:

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;

这篇关于Allegro中的平等运算符重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆