为什么我不能将唯一的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:\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/mingw32/bits/c++allocator.h:34:0, from c:\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/allocator.h:48, from c:\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/memory:64, from main.cpp:6:c:\mingw\bin\../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:\mingw\bin\../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 herec:\mingw\bin\../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:\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/ext/new_allocator.h:105:9: error: used hereIn file included from c:\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/vector:69:0, from main.cpp:7:
2 回答
慕标5832272
TA贡献1966条经验 获得超4个赞
unique_ptr
:
vec.push_back(std::move(ptr2x));
unique_ptr
unique_ptr
unique_ptr
unique_ptr
unique_ptr
std::unique_ptr<int> ptr(new int(1));
牛魔王的故事
TA贡献1830条经验 获得超3个赞
error: deleted function 'std::unique_ptr<_Tp, _Tp_Deleter>::uniqu e_ptr(const std::unique_ptr<_Tp, _Tp_Deleter>&) [with _Tp = int, _Tp_D eleter = std::default_delete<int>, std::unique_ptr<_Tp, _Tp_Deleter> = std::unique_ptr<int>]'
类满足MoveConstrucable和MoveAssignable的要求,但不满足CopyConstrucable或CopyAssignable的要求。
std::vector< std::unique_ptr< int > > vec;vec.emplace_back( new int( 1984 ) );
- 2 回答
- 0 关注
- 355 浏览
添加回答
举报
0/150
提交
取消