c++ - 将 unique_ptrs 从 map 插入 vector 时出错

标签 c++ c++11 c++14

我有一个 std::mapstd::stringstd::unique_ptr<BaseInt> .基本上我想要一个类名称为字符串 key 的 map 和一个唯一的指针作为相应的 value .并访问指针为 map["Derived1"]等(在下面的代码中解释)。

当我遍历 std::map 时并尝试推送每个 valuestd::vector ,我看到以下错误

Error C2280 'std::unique_ptr<BaseInt,std::default_delete<_Ty>>::unique_ptr(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)': attempting to reference a deleted function CreateInstanceFromList c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\xmemory

我在 Visual Studio 2017 版本 15.9.16 MSVC 14.16.27023

实现代码如下。 BaseInt是一个带有 int 的 BaseClass成员(member)和纯虚拟replaceInt() . DerivedInt1DerivedInt2用不同的 int 实现虚函数值和构造中的参数不同。

#include "UserClass.h"
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <memory>
#include <typeinfo>

typedef std::vector<std::unique_ptr<BaseInt>> vec_type;
typedef std::map<std::string, std::unique_ptr<BaseInt>> map_type;

template<typename T> std::unique_ptr<T> createInstance(vec_type& vec) { return std::make_unique<T>(); };
template<typename T, typename U> std::unique_ptr<T> createInstance(vec_type& vec, U u) { return std::make_unique<T>(u); };

void fillVector(map_type& map)
{
    vec_type my_vec;
    for (auto const& it : map )
    {
        std::cout << it.first << std::endl;
        it.second->replaceInt();

        //my_vec.emplace_back(std::move(it.second)); //this line gives error
    }
    // idea is to be able to access the pointer as map["Derived1"]
    std::cout << my_vec.size() << std::endl;
}

int main()
{
    map_type my_map;
    
    my_map.emplace("Derived1", createInstance<DerivedInt1>(my_vec, 7));
    my_map.emplace("Derived2", createInstance<DerivedInt2>(my_vec));

    fillVector(my_map);

    return 0;
}

我的直觉是我试图调用 unique_ptr 的复制构造函数但我实际上不知道如何。谢谢。

编辑:

所以,主要问题是 const&答案中提到的 @ALX23z 迭代器。以下更改有效:

    for (auto it = map.begin(); it != map.end(); ++it )
    {
        std::cout << it->first << std::endl;
        it->second->replaceInt();

        my_vec.emplace_back(std::move(it->second));
    }

编辑 2:

正如多人指出的那样,我犯了一个基本的设计缺陷,即不使用 unique_ptr作为独特的。我可以看到你提到的问题,我将研究更改为 shared_ptr 的可能性。或修改设计。感谢您的快速回复。我明天会在这里更新我的更改。

编辑 3:

查看主要项目场景后,我可以看到 std::map这里实际上只是一个一次性容器,所以我可以将它保存在本地并将所有所有权传递给 std::vector .

再次编辑:

有人对该问题投了反对票并标记为关闭它并提及

Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Reproducible Example.

我真的希望他们能启发我,让我了解为什么认为这种标记是必要的。我完全提供了重现我所面临的确切问题所必需的简短代码;而不是我实际正在处理的庞大项目代码。我提供了所有必要的信息,甚至包括我对失败之处的直觉。我添加了多个编辑信息,以便任何读者都能发现它有用。至少,发表评论并解释可以改进的地方。对不起,咆哮。

最佳答案

该行给出错误,因为您通过 const 迭代器迭代 map 。要应用 emplace_back,您需要一个非常量引用。

此外,您的方法总体上也存在错误。 unique_ptr 是一个unique 指针。不能有多个 unique_ptr 指向同一个对象。所以你不能在 map 和 vector 之间共享它们。

根据需要使用 2 个 shared_ptr 或带有原始指针的 unique_ptr。

关于c++ - 将 unique_ptrs 从 map 插入 vector 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58964729/

相关文章:

c++ - 为什么标准允许我在没有析构函数的情况下自由存储分配类?

c++ - 如何为 std::ostream_iterator 设置前缀?

c++ - 在 Visual C++ 控制台应用程序中显示图像?

c++ - 如何在 stdlib.h 中启用 __BEGIN_NAMESPACE_STD

c++ - 在 std::for_each 中调用 std::function

c++ - 将虚函数放入一个族中

c++ - 模板特化和引用

linux - Vim(spf13) 保存时自动调试

c++ - 使用 C++ 将 QML 元素动态添加/插入到 QML View 中

c++ - 在 vector 中搜索队列大小