No Match for operator <

I got this error message from the compiler: no match for ‘operator<’ (operand types are ‘const VP’ and ‘const VP’).
The problem is I actually implemented it. Here is my VP class.
I just want to use the VP for a priority_queue.


1
2
3
4
5
6
7
8
9
10
11
 class VP // Value Priority
{
    public:
        VP(int Value, int Priority): value(Value), priority(Priority) {};
        bool operator< (const VP& vp){
            return priority < vp.priority || 
                  (priority == vp.priority && value < vp.value);
        }
        int value;
        int priority;
};


More details about the error message:
/usr/include/c++/7/bits/stl_function.h:386:20: error: no match for ‘operator<’ (operand types are ‘const VP’ and ‘const VP’)
{ return __x < __y; }
~~~~^~~~~
In file included from TLB.cpp:1:0:
TLB.h:29:14: note: candidate: bool VP::operator<(const VP&) <near match>
bool operator< (const VP& vp)
Last edited on
You need to implement it as a free function, outside the class, that takes two parameters.
Never mind. I just find out that it is better to use pair instead of creating a class myself.
It should be possible to implement it as a member function. I think the reason that you got an error is because it wasn't marked as const.

 
bool operator< (const VP& vp) const {
Topic archived. No new replies allowed.