c++ - vector 中的自动指针(智能)

标签 c++ vector unique-ptr

我在解决以下代码时遇到问题。我知道由于复制问题,auto_ptr 不能在 STL 中使用。但是我也无法使用 C++11 unique_ptr 来解决这个问题。你能帮我解决这个问题吗?

错误:

    $ g++ -std=c++0x autoinvec.cpp
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/unique_ptr.h: In copy constructor âvna_data::vna_data(const vna_data&)â:
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/unique_ptr.h:214: error: deleted function âstd::unique_ptr<_Tp, _Tp_Deleter>::unique_ptr(const std::unique_ptr<_Tp, _Tp_Deleter>&) [with _Tp = MyClass, _Tp_Deleter = std::default_delete<MyClass>]â
autoinvec.cpp:11: error: used here
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/unique_ptr.h:214: error: deleted function âstd::unique_ptr<_Tp, _Tp_Deleter>::unique_ptr(const std::unique_ptr<_Tp, _Tp_Deleter>&) [with _Tp = MyClass, _Tp_Deleter = std::default_delete<MyClass>]â
autoinvec.cpp:11: error: used here
autoinvec.cpp: In function âint main()â:
autoinvec.cpp:39: note: synthesized method âvna_data::vna_data(const vna_data&)â first required here
autoinvec.cpp:39: error:   initializing argument 1 of âvoid Usethis::pushmydata(VNADATA)â
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/unique_ptr.h: In member function âvna_data& vna_data::operator=(const vna_data&)â:
autoinvec.cpp:11:   instantiated from âvoid std::vector<_Tp, _Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >, _Args&& ...) [with _Args = vna_data, _Tp = vna_data, _Alloc = std::allocator<vna_data>]â
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/vector.tcc:100:   instantiated from âvoid std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = vna_data, _Tp = vna_data, _Alloc = std::allocator<vna_data>]â
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_vector.h:747:   instantiated from âvoid std::vector<_Tp, _Alloc>::push_back(_Tp&&) [with _Tp = vna_data, _Alloc = std::allocator<vna_data>]â
autoinvec.cpp:27:   instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/unique_ptr.h:219: error: deleted function âstd::unique_ptr<_Tp, _Tp_Deleter>& std::unique_ptr<_Tp, _Tp_Deleter>::operator=(const std::unique_ptr<_Tp, _Tp_Deleter>&) [with _Tp = MyClass, _Tp_Deleter = std::default_delete<MyClass>]â
autoinvec.cpp:11: error: used here
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/unique_ptr.h:219: error: deleted function âstd::unique_ptr<_Tp, _Tp_Deleter>& std::unique_ptr<_Tp, _Tp_Deleter>::operator=(const std::unique_ptr<_Tp, _Tp_Deleter>&) [with _Tp = MyClass, _Tp_Deleter = std::default_delete<MyClass>]â
autoinvec.cpp:11: error: used here
In file included from /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/vector:69,
                 from autoinvec.cpp:3:
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/vector.tcc: In member function âvoid std::vector<_Tp, _Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >, _Args&& ...) [with _Args = vna_data, _Tp = vna_data, _Alloc = std::allocator<vna_data>]â:
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/vector.tcc:314: note: synthesized method âvna_data& vna_data::operator=(const vna_data&)â first required here

代码:

#include<iostream>
#include<memory>
#include<vector>
using namespace std;

class MyClass {
public:
 MyClass() { cout << "Myclass const" << endl; }
};

typedef struct vna_data {
 string i;
 auto_ptr<MyClass> ap1;
 auto_ptr<MyClass> ap2;
 string j;
 string m;
} VNADATA;

class Usethis {
 vector<VNADATA> _data;
public:
 Usethis() {cout << "Usethis const" << endl; }
 void pushmydata (VNADATA d);
};

void Usethis::pushmydata(VNADATA d) {
 _data.push_back(d);
}

int main () {

 Usethis u;
 VNADATA da;
 da.i = "one";
 da.j = "two";
 da.m = "three";
 da.ap1 = new MyClass();
 da.ap2 = new MyClass();
 u.pushmydata(da);

 return 0;
}

带有 Unique_ptr 的代码:

#include<iostream>
#include<memory>
#include<vector>
using namespace std;

class MyClass {
public:
 MyClass() { cout << "Myclass const" << endl; }
};

typedef struct vna_data {
 string i;
 unique_ptr<MyClass> ap1;
 unique_ptr<MyClass> ap2;
 string j;
 string m;
} VNADATA;

class Usethis {
 vector<VNADATA> _data;
public:
 Usethis() {cout << "Usethis const" << endl; }
 void pushmydata (VNADATA d);
};

void Usethis::pushmydata(VNADATA d) {
 _data.push_back(move(d));
}

int main () {

 Usethis u;
 VNADATA da;
 da.i = "one";
 da.j = "two";
 da.m = "three";
 da.ap1.reset( new MyClass );
 da.ap2.reset( new MyClass );
 u.pushmydata(da);

 return 0;
}

最佳答案

unique_ptr 可移动但不可复制。您的 Usethis::pushmydata 方法按值获取 VNADATA,它会尝试复制 unique_ptr,因此它会中断。

编译器指出您在第 39 行使用了 pushmydata 方法:

autoinvec.cpp: In function int main():
autoinvec.cpp:39: note: synthesized method vna_data::vna_data(const vna_data&) first required here
autoinvec.cpp:39: error:   initializing argument 1 of void Usethis::pushmydata(VNADATA)

要解决您的问题,请将 Usethis::pushmydata 的签名更改为;

void Usethis::pushmydata(VNADATA&& d)

关于c++ - vector 中的自动指针(智能),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17271647/

相关文章:

c++ - 在 VS2015 C++ 中打印我的用户名

c++ - 如何将成员的 protected 继承限制为仅一代或几代?

c++ - 3维 vector 的交集/可行性控制

c++ - 唯一指针映射,带有原始指针的 .at()

c++ - unique_ptr C++03仿真中的move函数

python - 无法将 cpplist 导入 Cython?

c++ - CString 到 char*

java - Android vector 图标-添加边框

c++ - 修复(锁定)std::vector 的大小

c++ - 具有 unique_ptr 的类的复制构造函数