代码之家  ›  专栏  ›  技术社区  ›  petersohn

矢量编译问题

  •  7
  • petersohn  · 技术社区  · 14 年前

    请考虑以下代码:

    #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;
    }
    

    当试图编译它时,我从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
    

    我觉得这里的警察有些问题。这是不是意味着 auto_ptr 不能用于 vector S?

    2 回复  |  直到 12 年前
        1
  •  18
  •   Björn Pollex    12 年前

    对的, std::auto_ptr 不能用于 std::vector .

    编译器抱怨的是有赋值运算符 auto_ptr 更改从中分配的对象,因此不能 const .

    你也要用 boost::ptr_vector 或向量 boost::shared_ptr S

        2
  •  5
  •   sharptooth    14 年前

    auto_ptr 有一个带有非常量参数的复制构造函数,因此编译器无法从 vector::push_back() 因为后者有常量参数。

    原因是当您初始化一个 自动PTR 实例与另一个实例之间新实例断开对象与另一个实例的连接,并将其与自身连接,从而避免在一个实例时出现悬空指针情况。 delete s对象和另一个对象仍然持有指向它的指针。