【问题标题】:Having trouble inserting values into a std::map with std::strings and std::vectors使用 std::strings 和 std::vectors 将值插入 std::map 时遇到问题
【发布时间】:2014-01-12 19:18:43
【问题描述】:

我会先发布我的代码然后解释我的问题:

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

您可能会看到,我正在尝试将 EntityVector 插入到 EntityVectorMap 中。但是,当我这样做时,我遇到了这个问题:

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>]'|

谢谢!

【问题讨论】:

  • 您正在插入entity_vector副本。这不起作用,因为元素是 unique_ptrs,即不可复制,因此 vector 本身是不可复制的。

标签: c++ c++11 vector stl stdmap


【解决方案1】:

m_entity_vector_map[key] = entity_vector 尝试复制 EntityVector 从而尝试复制 Entity 本质上是在复制 std::unique_ptr。你不能复制std::unique_ptr(它不再是唯一的了)。

您可能希望将entity_vector 移动到m_entity_vector_map,但是您不能将entity_vector 作为常量引用传递到pushEntityVector

【讨论】:

    猜你喜欢
    • 2019-08-19
    • 1970-01-01
    • 1970-01-01
    • 2019-05-07
    • 2023-01-13
    • 1970-01-01
    • 2010-09-10
    • 2011-01-19
    • 1970-01-01
    相关资源
    最近更新 更多