I've question to ask.
I've created a class called AstarPlanlama and have these 2 functions:
bool AstarPlanlama::nodeComp(const Node* lhs, const Node* rhs)
{
return lhs->F < rhs->F;
}
void AstarPlanlama::enKucukFliNodeBul(std::list<Node*> * OPEN)
{
std::list<Node*>::iterator it = std::min_element(OPEN->begin(), OPEN->end(), &AstarPlanlama::nodeComp);
OPEN->sort(&AstarPlanlama::nodeComp);
Q = OPEN->front();
OPEN->pop_front();
}
When I compile my code the error occurs in the xutility.h file.
template<class _Pr, class _Ty1, class _Ty2> inline
bool _Debug_lt_pred(_Pr _Pred,
_Ty1& _Left, _Ty2& _Right,
_Dbfile_t _File, _Dbline_t _Line)
{ // test if _Pred(_Left, _Right) and _Pred is strict weak ordering
if (!_Pred(_Left, _Right))
return (false);
else if (_Pred(_Right, _Left))
_DEBUG_ERROR2("invalid operator<", _File, _Line);
return (true);
}
Declerations of the functions:
bool nodeComp(const Node* lhs, const Node* rhs);
void enKucukFliNodeBul(std::list<Node*> * OPEN);
The error line is if (!_Pred(_Left, _Right))
What is wrong with the code?
Thank you for your returns..
My regards..
It looks like you are passing a member function as your custom comparator.
Make it static
or use std::bind
:
std::list<Node*>::iterator it = std::min_element(OPEN->begin(), OPEN->end(),
std::bind(&AstarPlanlama::nodeComp,
this,
std::placeholders::_1,
std::placeholders::_2));
OPEN->sort(std::bind(&AstarPlanlama::nodeComp,
this,
std::placeholders::_1,
std::placeholders::_2));
Member functions are special and need to be called on an object, this is why std::bind
is needed to bind to the this
pointer.
Your declaration of nodeComp()
is wrong - if you want to follow standard library conventions it needs to be either a free function or a static function, not a regular member function. Normally, I would recommend to make it a free function that's marked as a friend so it can gain access to private class members. The function as you declared it doesn't make the comparator function that's expected in xutility.h, as it carries the "hidden this" parameter, which is carried by all non-static member functions.
Also, given the implementation of nodeComp()
, there is no reason at all to make it a regular member function as it doesn't operate on its object's data, but on the data of objects that are being passed in as parameters.
In order to simplify both your life and the life of whoever else has to maintain the code after you, I would follow the standard library convention and replace nodeComp() with the following code and declare the operator a friend inside the class declaration of AstarPlanlama.
bool operator<(const AstarPlanlama::Node* lhs, const AstarPlanlama::Node* rhs)
{
return lhs->F < rhs->F;
}
Also, it's generally a bad idea to put raw pointers into standard library containers as that can lead to all sorts of interesting lifetime management issues. If you absolutely have to use pointers, I would recommend either using a container that's designed for it like the boost pointer containers, or wrap the object in a std::shared_ptr
instead of handling a raw pointer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With