Question

I have a problem creating a std::map<int, int> from a vector of pointers, called files. Each pointer points to an object with three member variables, one of which is size of type int. The key of the map would be the size and the value would be the number of objects which have the same size.

Do NOT waste your time on the second one! It is the next step in my program and I have figured it out already, I think. For initialization of the map I am using std::accumulate, because it returns a value. I am using std::tr1::shared_ptr for the pointers and a lambda expression for the predicate function. I am having problems with compilation.

Here's the code snippet:

map<int,int>* sizes = new map<int,int>();
sizes = accumulate(files.begin(), files.end(),sizes,
[&sizes](map<int,int> acc, shared_ptr<CFileType>& obj)
{
    return sizes->insert(pair<int,int>(obj->getSize(),0));
});

Here's the error:

error C2664: 'std::pair<_Ty1,_Ty2> `anonymous-namespace'::::operator ()(std::map<_Kty,_Ty>,std::tr1::shared_ptr &) const' : cannot convert parameter 1 from 'std::map<_Kty,_Ty> ' to 'std::map<_Kty,_Ty>'

I am not very sure what to pass to the lambda function. I have tried with pair<int, int>, but it didn't work. Also, I must mention that this map is returned to another function, so it has to be a pointer.

Any help from you would be appreciated. Thanks!


UPDATE:

Problem solved, here is the solution:

map<int,int>* sizes = accumulate(files.begin(), files.end(), new map<int,int>(),
    [](map<int,int>* acc, shared_ptr<CFileType>& obj)->map<int,int>*
    {
        acc->insert(pair<int,int>(obj->getSize(),0));
        return acc;
    });
Was it helpful?

Solution

The error message is that you have a type mismatch between the two kinds of std::maps. It looks like the error is in the code that calls the lambda, which apparently passes the wrong thing for the acc parameter. The good news is that the lambda, as posted, never actually uses the acc parameter.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top