2010-04-15 19 views
7

Si consideri il seguente codice:problemi di compilazione con vettore <auto_ptr<>>

#include <iostream> 
#include <memory> 
#include <vector> 

using namespace std; 

struct A 
{ 
    int a; 
    A(int a_):a(a_) {} 
}; 

int main() 
{ 
    vector<auto_ptr<A> > as; 
    for (int i = 0; i < 10; i++) 
    { 
     auto_ptr<A> a(new A(i)); 
     as.push_back(a); 
    } 
    for (vector<auto_ptr<A> >::iterator it = as.begin(); it != as.end(); ++it) 
     cout << (*it)->a << endl; 
} 

Quando si tenta di compilarlo, ottengo il seguente errore del compilatore oscuro da g ++:

g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/proba.d" -MT"src/proba.d" -o"src/proba.o" "../src/proba.cpp" 
/usr/include/c++/4.1.2/ext/new_allocator.h: In member function ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Tp*, const _Tp&) [with _Tp = std::auto_ptr<A>]’: 
/usr/include/c++/4.1.2/bits/stl_vector.h:606: instantiated from ‘void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = std::auto_ptr<A>, _Alloc = std::allocator<std::auto_ptr<A> >]’ 
../src/proba.cpp:19: instantiated from here 
/usr/include/c++/4.1.2/ext/new_allocator.h:104: error: passing ‘const std::auto_ptr<A>’ as ‘this’ argument of ‘std::auto_ptr<_Tp>::operator std::auto_ptr_ref<_Tp1>() [with _Tp1 = A, _Tp = A]’ discards qualifiers 
/usr/include/c++/4.1.2/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> >, const _Tp&) [with _Tp = std::auto_ptr<A>, _Alloc = std::allocator<std::auto_ptr<A> >]’: 
/usr/include/c++/4.1.2/bits/stl_vector.h:610: instantiated from ‘void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = std::auto_ptr<A>, _Alloc = std::allocator<std::auto_ptr<A> >]’ 
../src/proba.cpp:19: instantiated from here 
/usr/include/c++/4.1.2/bits/vector.tcc:256: error: passing ‘const std::auto_ptr<A>’ as ‘this’ argument of ‘std::auto_ptr<_Tp>::operator std::auto_ptr_ref<_Tp1>() [with _Tp1 = A, _Tp = A]’ discards qualifiers 
/usr/include/c++/4.1.2/bits/stl_construct.h: In function ‘void std::_Construct(_T1*, const _T2&) [with _T1 = std::auto_ptr<A>, _T2 = std::auto_ptr<A>]’: 
/usr/include/c++/4.1.2/bits/stl_uninitialized.h:86: instantiated from ‘_ForwardIterator std::__uninitialized_copy_aux(_InputIterator, _InputIterator, _ForwardIterator, __false_type) [with _InputIterator = __gnu_cxx::__normal_iterator<std::auto_ptr<A>*, std::vector<std::auto_ptr<A>, std::allocator<std::auto_ptr<A> > > >, _ForwardIterator = __gnu_cxx::__normal_iterator<std::auto_ptr<A>*, std::vector<std::auto_ptr<A>, std::allocator<std::auto_ptr<A> > > >]’ 
/usr/include/c++/4.1.2/bits/stl_uninitialized.h:113: instantiated from ‘_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<std::auto_ptr<A>*, std::vector<std::auto_ptr<A>, std::allocator<std::auto_ptr<A> > > >, _ForwardIterator = __gnu_cxx::__normal_iterator<std::auto_ptr<A>*, std::vector<std::auto_ptr<A>, std::allocator<std::auto_ptr<A> > > >]’ 
/usr/include/c++/4.1.2/bits/stl_uninitialized.h:254: instantiated from ‘_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>) [with _InputIterator = __gnu_cxx::__normal_iterator<std::auto_ptr<A>*, std::vector<std::auto_ptr<A>, std::allocator<std::auto_ptr<A> > > >, _ForwardIterator = __gnu_cxx::__normal_iterator<std::auto_ptr<A>*, std::vector<std::auto_ptr<A>, std::allocator<std::auto_ptr<A> > > >, _Tp = std::auto_ptr<A>]’ 
/usr/include/c++/4.1.2/bits/vector.tcc:279: 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> >, const _Tp&) [with _Tp = std::auto_ptr<A>, _Alloc = std::allocator<std::auto_ptr<A> >]’ 
/usr/include/c++/4.1.2/bits/stl_vector.h:610: instantiated from ‘void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = std::auto_ptr<A>, _Alloc = std::allocator<std::auto_ptr<A> >]’ 
../src/proba.cpp:19: instantiated from here 
/usr/include/c++/4.1.2/bits/stl_construct.h:81: error: passing ‘const std::auto_ptr<A>’ as ‘this’ argument of ‘std::auto_ptr<_Tp>::operator std::auto_ptr_ref<_Tp1>() [with _Tp1 = A, _Tp = A]’ discards qualifiers 
make: *** [src/proba.o] Error 1 

Mi sembra che non ci è un qualche tipo di problema con le mani qui. Questo significa che auto_ptr non può essere utilizzato in vector s?

+11

Non utilizzare mai un 'auto_ptr' in un contenitore. È una cosa sfortunata. :( – GManNickG

+2

Vedi questa domanda sul perché 'auto_ptr' non può essere usato con i contenitori STL: http://stackoverflow.com/questions/111478/why-is-it-wrong-to-use-stdauto-ptr-with-stl -containers – Naveen

+0

Perché non usi la ricerca? Esiste altre domande con queste idee –

risposta

18

corretto, std::auto_ptr non può essere utilizzato in std::vector.

Ciò che il compilatore lamenta è che l'operatore di assegnazione per auto_ptr modifica l'oggetto da cui viene assegnato e quindi non può essere const.

che si desidera utilizzare sia boost::ptr_vector o vettore di boost::shared_ptr s

+0

Grazie, quindi, andrò per boost :: shared_ptr, l'ho usato prima. – petersohn

+1

In realtà, se si ha accesso ad esso, 'unique_ptr' è molto meglio se non hai bisogno della proprietà condivisa, ma richiede semantica di spostamento C++ 0x. –

5

auto_ptr ha un costruttore di copia con un parametro non edificati, in modo che il compilatore non può chiamare da vector::push_back() in quanto quest'ultima ha parametro const.

Il motivo è quando si inizializza un auto_ptr esempio da un altro la nuova istanza disconnette l'oggetto dall'altra istanza e lo collega ad auto in modo da evitare una situazione puntatore penzoloni, quando un caso delete s l'oggetto e l'altro ancora in possesso di un puntatore ad esso.

Problemi correlati