向量< auto_ptr<>的编译问题> [英] Compilation problems with vector<auto_ptr<> >

查看:151
本文介绍了向量< 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> >如;
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 -MFsrc / proba。 d-MTsrc / proba.d-osrc / proba.o../src/proba.cpp
/usr/include/c++/4.1.2/ext/new_allocator.h :在成员函数'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:从'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:从这里实例
/usr/include/c++/4.1.2/ext/new_allocator.h:104:error:passing'const std :: auto_ptr< A>'as'this'参数'std :: auto_ptr< _Tp> :: operator std :: auto_ptr_ref< _Tp1>()[with _Tp1 = A,_Tp = A]'丢弃限定符
/usr/include/c++/4.1.2/bits/vector.tcc:在成员函数'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& >]':
/usr/include/c++/4.1.2/bits/stl_vector.h:610:从'void std :: vector <_Tp,_Alloc> :: push_back(const_Tp&与_Tp = std :: auto_ptr< A>,_Alloc = std :: allocator< std :: auto_ptr< A> >]'
../src/proba.cpp:19:从这里实例化
/usr/include/c++/4.1.2/bits/vector.tcc:256:error:passing'const std :: auto_ptr< A>'as'this'参数'std :: auto_ptr< _Tp> :: operator std :: auto_ptr_ref< _Tp1>()[with _Tp1 = A,_Tp = A]'丢弃限定符
/usr/include/c++/4.1.2/bits/stl_construct.h:在函数'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:从_ForwardIterator std :: __ uninitialized_copy_aux(_InputIterator,_InputIterator,_ForwardIterator,__false_type) _InputIterator = __gnu_cxx :: __ normal_iterator< std :: auto_ptr< A> *,std :: vector< std :: auto_ptr< A> ;, std :: allocator< std :: auto_ptr& > > >,_ForwardIterator = __gnu_cxx :: __ normal_iterator< std :: auto_ptr< A> *,std :: vector< std :: auto_ptr< A> ;, std :: allocator< std :: auto_ptr& > > >]'
/usr/include/c++/4.1.2/bits/stl_uninitialized.h:113:从'_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& > > >]'
/usr/include/c++/4.1.2/bits/stl_uninitialized.h:254:从'_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& > > >,_ForwardIterator = __gnu_cxx :: __ normal_iterator< std :: auto_ptr< A> *,std :: vector< std :: auto_ptr< A> ;, std :: allocator< std :: auto_ptr& > > >,_Tp = std :: auto_ptr< A>]'
/usr/include/c++/4.1.2/bits/vector.tcc:279:实例化为'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:实例化为'void std :: vector< _Tp,_Alloc> :: push_back(const _Tp& _Tp = std :: auto_ptr< A>,_Alloc = std :: allocator< std :: auto_ptr< A> >]'
../src/proba.cpp:19:从这里实例化
/usr/include/c++/4.1.2/bits/stl_construct.h:81:error:passing'const std :: auto_ptr< A>'as'this'参数'std :: auto_ptr< _Tp> :: operator std :: auto_ptr_ref< _Tp1>()[with _Tp1 = A,_Tp = A]'丢弃限定符
make:*** [src / proba.o]错误1

是const这里的一些问题。这是否意味着 auto_ptr 不能用于向量 s?

std :: auto_ptr 不能在 std :: vector 中使用 code>。



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



您要使用 boost :: ptr_vector 或向量 boost :: shared_ptr s


Consider the following code:

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

When trying to compile it, I get the following obscure compiler error from 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

It seems to me that there is some kind of problem with consts here. Does this mean that auto_ptr can't be used in vectors?

解决方案

Correct, std::auto_ptr cannot be used in std::vector.

What the compiler is complaining about there is the assignment operator for auto_ptr changes the object being assigned from, and so it cannot be const.

You want to use either boost::ptr_vector or a vector of boost::shared_ptrs

这篇关于向量&lt; auto_ptr&lt;&gt;的编译问题&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆