I overloaded the operator> but it still says that the operator is not executing operands

I need class B for a queue with a minimum priority of AToTime objects.

AToTime has operator>, and yet I get an error than there is no statement> matching 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;
};
+3
source share
2 answers
    bool operator >(const AToTime& other)

It must be a const function.

    bool operator >(const AToTime& other) const 
+9
source

Kenny's answer already shows you how to do this.

Note that I would prefer to implement binary operators that treat their operands the same way (they don't change them) as free functions:

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);}
+1

Source: https://habr.com/ru/post/1736869/


All Articles