Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overloading operator< for priority queue

I am trying to make a priority queue of a class I made like this -

std::priority_queue<Position> nodes;

I overloaded the < operator in Position like this -

bool Position::operator<(Position& right) {
    return (fvalue < right.getFValue());
}

However, whenever I try to compile I get this error message saying the < operator is not overloaded -

error: no match for ‘operator<’ in ‘__x < __y’
position.h:30: note: candidates are: bool Position::operator<(Position&)

What am I missing here? Any help is appreciated.

like image 787
Sterling Avatar asked Jun 23 '11 19:06

Sterling


1 Answers

Relational operators shouldn't change the operands. Try:

bool Position::operator<(const Position& right) const {

My guess is that either __x or __y (or both) are const. You can't call a non-const member function on __x if it's const, also you can't pass __y as the right parameter if __y is const and right is not.

like image 98
Ben Voigt Avatar answered Sep 18 '22 09:09

Ben Voigt