make your operator< const correct.
bool Node::operator< ( const Node&) const
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
vijayan's solution should be correct. There's a few things different between his and yours, though, which are important. First, his parameter takes a const object. This allows you to have a const Node on the right side of the expression. Second, he uses the pass-by-reference mechanism (the &) to avoid copying the node unnecessarily. Third, and the solution to your question, is the const at the end of the line which dictates that the function may be called for constant objects (which would be on the left-hand side).
Infarction
Posting Virtuoso
1,580 posts since May 2006
Reputation Points: 683
Solved Threads: 53
How are you currently sorting them?
Infarction
Posting Virtuoso
1,580 posts since May 2006
Reputation Points: 683
Solved Threads: 53
Actually, I have one more quick question. I'd actually like this to be a priority_queue of Node* (pointers to Node), but if I do that, then it sorts the queue using pointer arithmetic. So the Node*s with the lower addresses are put at the front and the higher ones are at the back. Is there any way for me to define a custom way to sort them in the Node class? Like, can I overload the "<" operator for just Node pointers somehow? Know what I mean?
pointers are a standard type and you can not define how pointers are to be
compared anywhere. however, you can tell the priority_queue of Node*
how you want priorities to be compared.
a. write a binary predicate to compare nodes
struct compare_node_pointers
{
bool operator() ( const Node* first, const Node* second ) const
{
// return true if first has lower priority than second.
// false otherwise.
}
};
b. specify the predicate to the priority_queue
typedef std::priority_queue< Node*, std::vector<Node*>,
compare_node_pointers > my_prioroty_queue ;
// use object(s) of type my_prioroty_queue where required
remember that the predicate must be a relation that is asymetric,
irreflexive and transitive; ie. one that imposes a strict oredering
on the sequence.
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
However, if I use the correct predicate: ...
the predicate you have written (as the correct one) is equivalent to
bool operator() ( const Node* first, const Node* second ) const
{
return first->getHeuristic() > second->getHeuristic() ;
// in all other cases you are returning false
}
the order is not involved at all.
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
Gah!!! No matter what I do, every time I make a correct implementation of this priority queue, when I try to run my program, I get this:
---------------------------
Microsoft Visual C++ Debug Library
---------------------------
Debug Assertion Failed!
Program: ...
File: c:\program files\microsoft visual studio 8\vc\include\algorithm
Line: 2025
Expression: invalid operator<
For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.
(Press Retry to debug the application)
---------------------------
Abort Retry Ignore
---------------------------
No matter what little tricks I try!
My current predicate looks like this:
struct comp
{
bool operator() ( const Node* first, const Node* second ) const
{
if (first->getHeuristic() > second->getHeuristic()) {
return true;
}
else if (second->getOrder() < first->getOrder()) {
return true;
}
else {
return false;
};
}
};
the only way this thing will run is if I make the < in the first else if statement a >, which orders them backwards. What the heck is going on???
i had said in my earlier replyremember that the predicate must be a relation that is asymetric,
irreflexive and transitive; ie. one that imposes a strict oredering
on the sequence. because it is important to understand it.
take two nodes
node_a ( getHeuristic returns 10, getOrder returns 6) and
node_b ( getHeuristic returns 11, getOrder returns 5)
comp()(a,b) returns true if (first->getHeuristic() > second->getHeuristic()) // false
else if (second->getOrder() < first->getOrder()) // true
comp()(b,a) also returns true
if (first->getHeuristic() > second->getHeuristic()) // true
this is illogical; as per your predicate, both the following statements are true:
one: node_a has a lower priority than node_b
two: node_b has a lower priority than node_a
which is absurd. (not asymetric!)
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
great. perhaps you would not be viewed as a newbie; certainly not in daniweb. thimgs like predicates are rocket science out here.
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287