2

Here is a bit of code that I'm currently using:

int Engine::getEntityCount(const int objectType)
{
    using namespace std;

    int total = 0;
    for_each(p_entities.begin(), p_entities.end(),
    [&objectType,&total](pair<const int, const list<Entity*>> pair)
    {   
        for_each((pair.second).begin(),(pair.second).end(),
        [&objectType,&total](Entity* &entity)
        {
            if ( entity->getAlive() == true && entity->getObjectType() == objectType )
                ++total;
        });
    });
    return total;
}

I'm getting the following error from intel c++:

error : function "lambda [](Entity *&)->void::operator()" cannot be called with the given argument list c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\algorithm

I'm having difficulty understanding what's wrong here. Does anyone have any ideas?

4
  • 1
    Is this really the most elegant way to code a loop over a container? If you want to be cutting-edge, why not range-based for? Commented Dec 3, 2012 at 6:05
  • @Potatoswatter You're right. I've just used this method because it seems the vast amount of c++ compilers at the moment support lambda, but not range based for atm. I'm also curious as to why I'm getting this error in the first place. Commented Dec 3, 2012 at 6:07
  • @dk123, If you're interested, VS2012 supports ranged-based for. GCC and Clang have since before that. Commented Dec 3, 2012 at 6:08
  • @Potatoswatter I guess I should give VS2012 a shot. Thanks for the quick replies; they've cleared everything. Commented Dec 3, 2012 at 6:27

2 Answers 2

2

You're asking for a non-const reference to a pointer to an Entity. The list containing that pointer is const. You must decide between a non-const pointer or a const list.

Sign up to request clarification or add additional context in comments.

4 Comments

You're right. Which is why I originally had the code as 'const Entity*'. I get this error if I do: error : function "lambda [](const Entity *&)->void::operator()" cannot be called with the given argument list.
@dk123 You need Entity *const &
@dk123: That gives you a reference to a non-const pointer to a const Entity. You want a reference to a const pointer to a non-const Entity: Entity * const &
@Potatoswatter Thanks for the quick replies. I should revise my const usage.
-1
....
    for_each((pair.second).begin(),(pair.second).end(),
    [&objectType,&total](const Entity* entity)
    {
        ....
    });
});

....

Comments

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.