• Advertisement

• Popular Now

• 10
• 9
• 13
• 10
• 18
• Advertisement
• Advertisement
• Advertisement

Trouble with priority_queue

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

If you intended to correct an error in the post then please contact us.

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 this post

Share on other sites
Advertisement
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 this post

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:
Unless I've misunderstood something about operator overloading a constant member function should work as well.
bool Node::operator <(const Node &rhs) const { return f > rhs.f;}

Share this post

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 this post

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

Share this post

Share on other sites

• Advertisement