重载运算符<用于优先级队列
Overloading operator< for priority queue
我正在尝试为我这样创建的类创建一个优先级队列-
std::priority_queue<Position> nodes;
我使<操作员处于这样的位置-
bool Position::operator<(Position& right) {
return (fvalue < right.getFValue());
}
然而,每当我尝试编译时,我都会收到一条错误消息,上面写着<操作员没有过载-
error: no match for ‘operator<’ in ‘__x < __y’
position.h:30: note: candidates are: bool Position::operator<(Position&)
我在这里错过了什么?感谢您的帮助。
关系运算符不应该更改操作数。尝试:
bool Position::operator<(const Position& right) const {
我的猜测是__x
或__y
(或两者)都是const
。如果__x
是const
,则不能在其上调用非常数成员函数;如果__y
是const
,而right
不是,则不能将__y
作为right
参数传递。
-
最好不要为了满足集合而重载比较运算符。(http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Operator_Overloading)。如果可能的话,最好使用比较器对象或函数。如果你的运算符定义了这些对象的内在顺序,而不仅仅是它们在队列中的优先级,那么这是可以的。但如果不是,如果另一个开发人员在另一个上下文中使用你的运算符,你可能会遇到麻烦。
-
我不确定
fvalue
是什么,但可能没有为它定义operator <
相关文章:
- 1d 智能指针不适用于语法 (*)++
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 用于访问容器<T>数据成员的正确 API
- 重载操作程序时出错>>用于类中的字符串 memebr
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- C++中的cin.ignore()函数不适用于整个流
- C++优先级队列,按对象的唯一指针的特定方法升序排列
- 没有用于初始化C++中的变量模板的匹配构造函数
- 用于C++中带有数组和指针的循环
- 按对象的特定方法按升序排列的C++优先级队列
- 使用2个键的cpp-stl::优先级队列排序不正确
- 为什么它不适用于Visual 2019的原因
- 使用在用于SFINAE的void_t中具有参数的方法
- 在createdialog创建的窗口中捕获用于编辑控件的OnMouseMove消息
- 选择用于赋值初始化的转换函数的优先级
- 用于将优先级链接移动到列表标题的算法
- c++一个固定大小的优先级队列,用于存储k个最近邻居
- 如何正确地将std::函数用于其他数据类型(在优先级队列中)
- c++中用于字符串优先级队列的未排序双链表
- 重载运算符<用于优先级队列