我重载了运算符 >但它仍然说没有运算符匹配操作数

发布于 2024-08-25 05:40:46 字数 711 浏览 3 评论 0原文

我需要 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绾颜 2024-09-01 05:40:46
    bool operator >(const AToTime& other)

它应该是一个const函数。

    bool operator >(const AToTime& other) const 
    bool operator >(const AToTime& other)

It should be a const function.

    bool operator >(const AToTime& other) const 
违心° 2024-09-01 05:40:46

肯尼的答案已经向您展示了如何进行这项工作。

请注意,我更愿意实现二元运算符,将其操作数平等地对待(它们不修改它们)作为自由函数:

inline bool operator>(const AToTime& khs, const AToTime& rhs)
{
  return lhs.m_time > rhs.m_time;
}

此外,通常用户希望所有关系运算符都存在(如果其中一个存在)。由于 std 库主要需要 operator<,除了相等之外,我会在 operator< 之上实现其他库:

inline bool operator<(const AToTime& khs, const AToTime& rhs)
{return lhs.m_time < rhs.m_time;}

inline bool operator>(const AToTime& khs, const AToTime& rhs)
{return rhs < lhs;}

inline bool operator<=(const AToTime& khs, const AToTime& rhs)
{return !(lhs > rhs);}

inline bool operator>=(const AToTime& khs, const AToTime& rhs)
{return !(lhs < rhs);}

inline bool operator==(const AToTime& khs, const AToTime& rhs)
{return lhs.m_time == rhs.m_time;}

inline bool operator!=(const AToTime& khs, const AToTime& rhs)
{return !(lhs.m_time == rhs.m_time);}

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:

inline bool operator>(const AToTime& khs, const AToTime& rhs)
{
  return lhs.m_time > rhs.m_time;
}

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 of operator<:

inline bool operator<(const AToTime& khs, const AToTime& rhs)
{return lhs.m_time < rhs.m_time;}

inline bool operator>(const AToTime& khs, const AToTime& rhs)
{return rhs < lhs;}

inline bool operator<=(const AToTime& khs, const AToTime& rhs)
{return !(lhs > rhs);}

inline bool operator>=(const AToTime& khs, const AToTime& rhs)
{return !(lhs < rhs);}

inline bool operator==(const AToTime& khs, const AToTime& rhs)
{return lhs.m_time == rhs.m_time;}

inline bool operator!=(const AToTime& khs, const AToTime& rhs)
{return !(lhs.m_time == rhs.m_time);}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文