# Trouble with priority_queue

This topic is 4657 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am writing an A* search procedure and define my nodes like this:
struct Node
{
int x,y;
unsigned char data;

int f,g,h;

bool closed;

Node* parent;

bool operator<( Node & rhs )
{
return (this->f >= rhs.f);
}
};


Then I try to use a priority_queue of these like this:
Quote:
 std::priority_queue open_list;
The error I get is this:
Quote:
 binary '<' : no operator defined which takes a left-hand operand of type 'const std::vector<_Ty>::value_type' (or there is no acceptable conversion) To use the operator, you must overload it for the specified type or define a conversion to a type for which the operator is defined.
I don't understand this because I do overload the less than operator. Did I overload it incorrectly? Is there Somthing else I need to do? Thanks for reading this! [Edited by - Simian Man on September 22, 2005 3:42:04 PM]

##### Share on other sites
EDIT: This answer was incorrect. See the following posts.

priority_queue wants a binary operator<, that is a free function which takes two parameters, not a member function:
struct Node{	int x,y;	unsigned char data;	int f,g,h;	bool closed;	Node* parent;};bool operator<(Node const & lhs, Node const & rhs){	return lhs.f >= rhs.f;}

Enigma

[Edited by - Enigma on September 22, 2005 4:43:26 PM]

Thank You!

##### Share on other sites
Quote:
 Original post by Enigmapriority_queue wants a binary operator<, that is a free function which takes two parameters, not a member function:
bool Node::operator <(const Node &rhs) const { return f > rhs.f;}

##### Share on other sites
Quote:
 Original post by Enigmapriority_queue wants a binary operator<, that is a free function which takes two parameters, not a member function:*** Source Snippet Removed ***Enigma

That is not the problem, the problem is that he did not allow his operands to be const objects. The default function object used by priority_queue is ::std::less, which performs a less then comparison using references to const values. Since his version accepts operands by reference to non-const, he has error.

That being said, one should still generally prefer non-member functions for operators such as < for symmetry in syntax and in semantics.

##### Share on other sites
Yes, you're right of course. Must have been having a brain-fart. My apologies. It's the constness that was the issue.

Enigma

1. 1
2. 2
3. 3
4. 4
Rutin
17
5. 5

• 12
• 9
• 12
• 37
• 12
• ### Forum Statistics

• Total Topics
631419
• Total Posts
2999986
×