bak*_*kar 5 c++ debugging templates
我有问题要问。
我创建了一个名为 AstarPlanlama 的类并具有以下两个功能:
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();
}
Run Code Online (Sandbox Code Playgroud)
当我编译我的代码时,错误发生在xutility.h文件中。
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);
}
Run Code Online (Sandbox Code Playgroud)
函数的声明:
bool nodeComp(const Node* lhs, const Node* rhs);
void enKucukFliNodeBul(std::list<Node*> * OPEN);
Run Code Online (Sandbox Code Playgroud)
错误行是if (!_Pred(_Left, _Right))
代码有什么问题?
谢谢你的回报..
致以我的问候..
看起来您正在传递一个成员函数作为自定义比较器。制作static或使用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));
Run Code Online (Sandbox Code Playgroud)
成员函数很特殊,需要在对象上调用,这就是为什么std::bind需要绑定到this指针。