0
votes

I have a class called Info.

I tried creating a unique_ptr vector to hold a list of this class.

The map contains the key as a string and unique_ptr as the value.

But when I try retrieving the value from the map and try to put it into a vector to form the list, VS2010 compiler gives an error : error C2664: 'void std::vector<_Ty>::push_back(_Ty &&)' : cannot convert parameter 1 from 'std::vector<_Ty>' to 'std::unique_ptr<_Ty> &&'

vector<unique_ptr<Info>> sInfo;
QMap<QString,vector<unique_ptr<Info>>>::Iterator iterMap;
for(iterMap = map_sInfo.begin(); iterMap != map_sInfo.end(); ++iterMap)
{
    vector<unique_ptr<Info>>sValue = iterMap.value();
    sInfo.push_back((sValue)); //error C2664
}

How do I make the Any help regarding this would be much appreciated.

1

1 Answers

1
votes

As the error message suggests, you're trying to push back the whole sValue container (which is a vector<unique_ptr<Info>> instead of a unique_ptr<Info>. As you just noticed, that isn't going too well.

You have a few options. First, you can loop over the content of sValue and push_back every element. Second, you can use std::copy to add the elements in one statement:

std::copy(sValue.begin(), sValue.end(), std::back_inserter(sInfo);

A third option is to use std::vector's insert function to add the whole container in one go:

sInfo.insert(sInfo.end(), sValue.begin(), sValue.end());