我重载了运算符 >但它仍然说没有运算符匹配操作数
我需要 B 类有一个 AToTime 对象的最小优先级队列。
AToTime 有operator>
,但我收到错误告诉我没有operator>;匹配操作数...
#include <queue>
#include <functional>
using namespace std;
class B
{
public:
B();
virtual ~B();
private:
log4cxx::LoggerPtr m_logger;
class AToTime
{
public:
AToTime(const ACE_Time_Value& time, const APtr a) : m_time(time), m_a(a){}
bool operator >(const AToTime& other)
{
return m_time > other.m_time;
}
public:
ACE_Time_Value m_time;
APtr m_a;
};
priority_queue<AToTime, vector<AToTime>, greater<AToTime> > m_myMinHeap;
};
I need B class to have a min priority queue of AToTime objects.
AToTime have operator>
, and yet I receive error telling me than there is no operator> matching the operands...
#include <queue>
#include <functional>
using namespace std;
class B
{
public:
B();
virtual ~B();
private:
log4cxx::LoggerPtr m_logger;
class AToTime
{
public:
AToTime(const ACE_Time_Value& time, const APtr a) : m_time(time), m_a(a){}
bool operator >(const AToTime& other)
{
return m_time > other.m_time;
}
public:
ACE_Time_Value m_time;
APtr m_a;
};
priority_queue<AToTime, vector<AToTime>, greater<AToTime> > m_myMinHeap;
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它应该是一个const函数。
It should be a const function.
肯尼的答案已经向您展示了如何进行这项工作。
请注意,我更愿意实现二元运算符,将其操作数平等地对待(它们不修改它们)作为自由函数:
此外,通常用户希望所有关系运算符都存在(如果其中一个存在)。由于 std 库主要需要
operator<
,除了相等之外,我会在operator<
之上实现其他库:Kenny's answer already shows you how to make this work.
Note that I would prefer to implement binary operators which treat their operands equally (they're not modifying them) as free functions:
Further, usually users expect all relational operators to be present if one of them is there. Since the std library mostly wants
operator<
, except for equality I'd implement the others on top ofoperator<
: