Allegro中的相等运算符重载
Equality Operator Overloading in Allegro
我的一个朋友试图重载一个相等操作符来比较Allegro中的颜色,但是它不起作用,他得到错误"no match for operator=="这是在Color类/结构之外重载的,重载的操作符函数如下所示:
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_COLOR
之外实现,对吧?如何解决这个问题,是否可以在Allegro Color结构体之外重载
operator==
是一个二进制运算符,但是您只有一个参数。试试这个:
bool operator==(const Color& _col, const Color& rhs) { ... }
后记:此格式的代码:
if ( condition )
return true;
else
return false;
在c++中是不必要的冗长。最好这样做:
return condition;
就你的情况而言,我更希望看到:
return _col.a==rhs.a && _col.b==rhs.b && _col.g==rhs.g && _col.r==rhs.r;
相关文章:
- 使用C++中的模板和运算符重载执行矩阵运算
- 为什么这个运算符<重载函数对 STL 算法不可见?
- <T> 通过模板化运算符重载将 std::complex 乘以双倍
- C++20概念:需要运算符重载
- 使用赋值运算符重载从类中返回jobject
- 在运算符重载定义中使用成员函数(const错误)
- 字节到位运算符重载C++
- 为什么在运算符重载时需要参考?
- 类中 c++ 的运算符 + 重载
- 算术复合运算符重载为非成员
- 运算符重载 (+),用于添加两个具有 C++ 的数组
- 交换运算符 + 重载会导致无限递归
- 如何理解新的运算符重载?
- 向量保持复数的运算符重载
- 如何创建运算符重载?
- 链接列表运算符重载没有打印出我想要的内容
- C++:需要帮助了解运算符重载错误
- 使用模板化运算符重载 XOR 运算符失败
- 如何确保接受的C++模板类型使运算符重载?
- 运算符重载使用运算符+添加类模板