Search code examples
c++structoperator-overloadingpriority-queue

"No match for operator>" when using priority queue in C++


#include<bits/stdc++.h>
using namespace std;

struct vert
{
    int n,m;
    vert(int Node1=0,int Node2=0)
    {
        n=Node1,m=Node2;
    }
    bool operator<(vert const &obj)
    {
            return m<obj.m;
    }
};

int main()
{
    priority_queue<vert> q;
    q.push(vert(1,2));
}

when I run this code, I get the following error, error: no match for 'operator<' (operand types are 'const vert' and 'const vert'), I even declare what the operator < does, but it still doesn't work, How do I fix this?


Solution

  • As the error message says, use const.

    Try this

    bool operator<(vert const &obj) const