嗯,這是我第一次使用lambda的經驗,我知道我可能會濫用這個功能,但我想知道爲什麼我應該得到一個編譯錯誤?C++ lambda錯誤:沒有適當的默認構造函數available
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 ==========
這是你想要直接編譯的代碼?請製作[SSCCE](http://sscce.org/)。 – GManNickG
@GManNickG現在它是我試圖編譯的確切函數。一切工作正常,除了lambda部分。 – Ali1S232
@Gajet:刪除與問題無關的位,也不顯示上下文。 –