gpt4 book ai didi

C++ lambda 错误 : no appropriate default constructor available

转载 作者:搜寻专家 更新时间:2023-10-31 00:43:25 24 4
gpt4 key购买 nike

嗯,这是我第一次使用 lambda,我知道我可能会误用这个功能,但我想知道为什么我会遇到编译错误?

GameMap::MoveDirection AIPlayer::getSafeRouteTo(const GameMap::PlayerInfo& s, const Point& e)
{
struct node : public Point
{
GameMap::MoveDirection parent;
float d;
node():Point(){};
node(Point p) : Point(p)
{
};
};
auto lambdaFunction = [this,&e](node&left,node&right)->bool
{
return this->getDistance(left,e) + left.d < this->getDistance(right,e) + right.d;
};
priority_queue<node,vector<node>, decltype(lambdaFunction)> Q;
//do stuff
return GameMap::MD_None;
}

为了论证,让我说 MoveDirection 是一个枚举,而 Point 确实有一个默认构造函数。正如我通过删除该特定行评论的那样,我没有收到错误,但我真的需要它在那里。这是 VC2010 生成的错误:

d:\program files (x86)\microsoft visual studio 10.0\vc\include\queue(225): error C2512: '`anonymous-namespace'::<lambda0>' : no appropriate default constructor available
1> d:\program files (x86)\microsoft visual studio 10.0\vc\include\queue(223) : while compiling class template member function 'std::priority_queue<_Ty,_Container,_Pr>::priority_queue(void)'
1> with
1> [
1> _Ty=AIPlayer::getSafeRouteTo::node,
1> _Container=std::vector<AIPlayer::getSafeRouteTo::node>,
1> _Pr=`anonymous-namespace'::<lambda0>
1> ]
1> c:\users\ali\documents\visual studio 2010\projects\bokhorbokhor\aiplayer.cpp(147) : see reference to class template instantiation 'std::priority_queue<_Ty,_Container,_Pr>' being compiled
1> with
1> [
1> _Ty=AIPlayer::getSafeRouteTo::node,
1> _Container=std::vector<AIPlayer::getSafeRouteTo::node>,
1> _Pr=`anonymous-namespace'::<lambda0>
1> ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

最佳答案

lambda 不是默认可构造的。您必须将它传递给 priority_queue 的构造函数:

explicit priority_queue(const Compare& x = Compare(), Container&& = Container());

像这样:

priority_queue<node,vector<node>, decltype(lambdaFunction)> Q ( lambdaFunction );

关于C++ lambda 错误 : no appropriate default constructor available,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10644575/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com