Note that there are some explanatory texts on larger screens.

plurals
  1. POAuto pointer (smart) in vector
    primarykey
    data
    text
    <p>I am having trouble in solving the below code. I understand auto_ptr cannot be used in STL due to the copy issue. But I am not able to solve this using the C++11 unique_ptr as well. Can you please help me solve this?</p> <p>Error:</p> <pre><code> $ 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&amp;)â: /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&lt;_Tp, _Tp_Deleter&gt;::unique_ptr(const std::unique_ptr&lt;_Tp, _Tp_Deleter&gt;&amp;) [with _Tp = MyClass, _Tp_Deleter = std::default_delete&lt;MyClass&gt;]â 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&lt;_Tp, _Tp_Deleter&gt;::unique_ptr(const std::unique_ptr&lt;_Tp, _Tp_Deleter&gt;&amp;) [with _Tp = MyClass, _Tp_Deleter = std::default_delete&lt;MyClass&gt;]â 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&amp;)â 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&amp; vna_data::operator=(const vna_data&amp;)â: autoinvec.cpp:11: instantiated from âvoid std::vector&lt;_Tp, _Alloc&gt;::_M_insert_aux(__gnu_cxx::__normal_iterator&lt;typename std::_Vector_base&lt;_Tp, _Alloc&gt;::_Tp_alloc_type::pointer, std::vector&lt;_Tp, _Alloc&gt; &gt;, _Args&amp;&amp; ...) [with _Args = vna_data, _Tp = vna_data, _Alloc = std::allocator&lt;vna_data&gt;]â /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/vector.tcc:100: instantiated from âvoid std::vector&lt;_Tp, _Alloc&gt;::emplace_back(_Args&amp;&amp; ...) [with _Args = vna_data, _Tp = vna_data, _Alloc = std::allocator&lt;vna_data&gt;]â /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&lt;_Tp, _Alloc&gt;::push_back(_Tp&amp;&amp;) [with _Tp = vna_data, _Alloc = std::allocator&lt;vna_data&gt;]â 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&lt;_Tp, _Tp_Deleter&gt;&amp; std::unique_ptr&lt;_Tp, _Tp_Deleter&gt;::operator=(const std::unique_ptr&lt;_Tp, _Tp_Deleter&gt;&amp;) [with _Tp = MyClass, _Tp_Deleter = std::default_delete&lt;MyClass&gt;]â 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&lt;_Tp, _Tp_Deleter&gt;&amp; std::unique_ptr&lt;_Tp, _Tp_Deleter&gt;::operator=(const std::unique_ptr&lt;_Tp, _Tp_Deleter&gt;&amp;) [with _Tp = MyClass, _Tp_Deleter = std::default_delete&lt;MyClass&gt;]â 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&lt;_Tp, _Alloc&gt;::_M_insert_aux(__gnu_cxx::__normal_iterator&lt;typename std::_Vector_base&lt;_Tp, _Alloc&gt;::_Tp_alloc_type::pointer, std::vector&lt;_Tp, _Alloc&gt; &gt;, _Args&amp;&amp; ...) [with _Args = vna_data, _Tp = vna_data, _Alloc = std::allocator&lt;vna_data&gt;]â: /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/vector.tcc:314: note: synthesized method âvna_data&amp; vna_data::operator=(const vna_data&amp;)â first required here </code></pre> <p>Code:</p> <pre><code>#include&lt;iostream&gt; #include&lt;memory&gt; #include&lt;vector&gt; using namespace std; class MyClass { public: MyClass() { cout &lt;&lt; "Myclass const" &lt;&lt; endl; } }; typedef struct vna_data { string i; auto_ptr&lt;MyClass&gt; ap1; auto_ptr&lt;MyClass&gt; ap2; string j; string m; } VNADATA; class Usethis { vector&lt;VNADATA&gt; _data; public: Usethis() {cout &lt;&lt; "Usethis const" &lt;&lt; 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; } </code></pre> <p>Code with Unique_ptr:</p> <pre><code>#include&lt;iostream&gt; #include&lt;memory&gt; #include&lt;vector&gt; using namespace std; class MyClass { public: MyClass() { cout &lt;&lt; "Myclass const" &lt;&lt; endl; } }; typedef struct vna_data { string i; unique_ptr&lt;MyClass&gt; ap1; unique_ptr&lt;MyClass&gt; ap2; string j; string m; } VNADATA; class Usethis { vector&lt;VNADATA&gt; _data; public: Usethis() {cout &lt;&lt; "Usethis const" &lt;&lt; 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; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload