2

Well this is my first experience with lambda, and I know I might be misusing this feature but I'm wondering why should I get a compile error?

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;
}

and for the sake of argument let me say that MoveDirection is an enum, and Point does have a default constructor. as I commented by removing that specific line I don't get the error but I really need it to be there. here are the errors VC2010 is generating:

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 ==========
3
  • 1
    Is this the code you're trying to compile directly? Please make a SSCCE. Commented May 17, 2012 at 23:01
  • @GManNickG now it's the exact function I'm trying to compile. everything works fine except the lambda part. Commented May 17, 2012 at 23:06
  • @Gajet: removed the bits that aren't relevant to the problem nor show context. Commented May 17, 2012 at 23:08

1 Answer 1

6

The lambda is not default constructible. You have to pass it to this constructor of the priority_queue:

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

Like this:

priority_queue<node,vector<node>, decltype(lambdaFunction)> Q ( lambdaFunction );
Sign up to request clarification or add additional context in comments.

2 Comments

it seems it's now being compiled though I had to put parentheses instead of {}
@Gajet : Right, VC++ does not support uniform initialization syntax.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.