为什么我不能将 unique_ptr 推回到向量中?

如何解决为什么我不能将 unique_ptr 推回到向量中??

开发过程中遇到为什么我不能将 unique_ptr 推回到向量中?的问题如何解决?下面主要结合日常开发的经验,给出你关于为什么我不能将 unique_ptr 推回到向量中?的解决方法建议,希望对你解决为什么我不能将 unique_ptr 推回到向量中?有所启发或帮助;

问题描述

这个程序有什么问题?

#include <memory>
#include <vector>

int main()
{
    std::vector<std::unique_ptr<int>> vec;

    int x(1);
    std::unique_ptr<int> ptr2x(&x);
    vec.push_back(ptr2x); //This tiny command has a vicious error.

    return 0;
}

错误:

In file included from c:mingwin../lib/gcc/mingw32/4.5.0/include/c++/mingw32/bits/c++allocator.h:34:0,from c:mingwin../lib/gcc/mingw32/4.5.0/include/c++/bits/allocator.h:48,from c:mingwin../lib/gcc/mingw32/4.5.0/include/c++/memory:64,from main.cpp:6:
c:mingwin../lib/gcc/mingw32/4.5.0/include/c++/bits/unique_ptr.h: In member function 'void __gnu_cxx::new_allocator<_Tp>::construct(_Tp*,const _Tp&) [with _Tp = std::unique_ptr<int>,_Tp* = std::unique_ptr<int>*]':
c:mingwin../lib/gcc/mingw32/4.5.0/include/c++/bits/stl_vector.h:745:6:   instantiated from 'void std::vector<_Tp,_Alloc>::push_back(const value_type&) [with _Tp = std::unique_ptr<int>,_Alloc = std::allocator<std::unique_ptr<int> >,value_type = std::unique_ptr<int>]'
main.cpp:16:21:   instantiated from here
c:mingwin../lib/gcc/mingw32/4.5.0/include/c++/bits/unique_ptr.h:207:7: error: deleted function 'std::unique_ptr<_Tp,_Tp_Deleter>::unique_ptr(const std::unique_ptr<_Tp,_Tp_Deleter>&) [with _Tp = int,_Tp_Deleter = std::default_delete<int>,std::unique_ptr<_Tp,_Tp_Deleter> = std::unique_ptr<int>]'
c:mingwin../lib/gcc/mingw32/4.5.0/include/c++/ext/new_allocator.h:105:9: error: used here
In file included from c:mingwin../lib/gcc/mingw32/4.5.0/include/c++/vector:69:0,from main.cpp:7:
c:mingwin../lib/gcc/mingw32/4.5.0/include/c++/bits/unique_ptr.h: In member function 'void std::vector<_Tp,_Alloc>::_M_insert_aux(std::vector<_Tp,_Alloc>::iterator,_Args&& ...) [with _Args = {const std::unique_ptr<int>&},_Tp = std::unique_ptr<int>,std::vector<_Tp,_Alloc>::iterator = __gnu_cxx::__normal_iterator<std::unique_ptr<int>*,std::vector<std::unique_ptr<int> > >,typename std::vector<_Tp,_Alloc>::_Base::_Tp_alloc_type::pointer = std::unique_ptr<int>*]':
c:mingwin../lib/gcc/mingw32/4.5.0/include/c++/bits/stl_vector.h:749:4:   instantiated from 'void std::vector<_Tp,_Tp_Deleter> = std::unique_ptr<int>]'
c:mingwin../lib/gcc/mingw32/4.5.0/include/c++/bits/vector.tcc:314:4: error: used here

尚未找到解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

编程问答相关问答

是否可以将 Python 程序转换为 C/C++? 我需要实现几个算法,我不确定性能差距是否足以证明我在 C/C++ 中执行它时所经历的所有痛苦(我不擅长)).我想写一个简单的算法,并根据这样一个转换后的解决方案对其进行基准测
我想使用 NTT 进行快速平方(请参阅快速 bignum 平方计算),但即使对于非常大的数字……超过 12000 位.
以下代码: myQueue.enqueue(\'a\'); myQueue.enqueue(\'b\'); cout << myQueue.dequeue() << myQueue.dequeue();
据我所知,写时复制不是在 C++11 中实现符合标准的 std::string 的可行方法,但是当它最近在讨论中出现时,我发现我自己无法直接支持这种说法.
这篇文章的评论部分有一个关于使用 std::vector::reserve 的帖子() vs. std::vector::resize().
我了解 inline 本身是对编译器的建议,它可以自行决定是否内联函数,并且还会生成可链接的目标代码.
我最近遇到了一个问题 可以使用模数除法轻松解决,但输入是浮点数: 给定一个周期函数(例如sin)和一个只能在周期范围内计算它的计算机函数(例如[-π,π]),制作一个可以处理任何输入的函数.
我想了解某个函数在我的 C++ 程序中在 Linux 上执行所需的时间.之后,我想做一个速度比较.我看到了几个时间函数,但最终从 boost 得到了这个.时间:
微信公众号搜索 “ 程序精选 ” ,选择关注!
微信公众号搜 "程序精选"关注