简体   繁体   中英

Having trouble inserting values into a std::map with std::strings and std::vectors

I will post my code first then explain my issue:

typedef std::unique_ptr<SEntity> Entity;
typedef std::vector<Entity> EntityVector;
typedef std::map<std::string, EntityVector> EntityVectorMap;

const void pushEntityVector(const std::string& key, const EntityVector& entity_vector)
{
    m_entity_vector_map[key] = entity_vector;
}

As you can probably see, I'm trying to insert an EntityVector into the EntityVectorMap. I'm hit with this issue when I do this however:

c:\program files (x86)\codeblocks\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_algobase.h|335|error: use of deleted function 'std::unique_ptr<_Tp, _Dp>& std::unique_ptr<_Tp, _Dp>::operator=(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = SE::SEntity; _Dp = std::default_delete<SE::SEntity>]'|

Thanks!

m_entity_vector_map[key] = entity_vector tries to copy an EntityVector thereby trying to copy an Entity which is essentially copying a std::unique_ptr . You cannot copy std::unique_ptr (it wouldn't be unique anymore).

You might want to move entity_vector into m_entity_vector_map , but then you cannot pass entity_vector as a const reference into pushEntityVector .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM