linux怎么编译sharedptr,linux使用shared_ptr出现错误

我在不同的系统下采用下面的方式使用shared_ptr以及vector:

std::vector > pointer_arrays;

std::vector >::const_iterator pointer_Iter;

并采用如下初始化:

const std::shared_ptr object(new Object);

wordDataTab.push_back(object);

这样在mac os下面编译,通过并且获得正确的结果。但是我在centos 下的使用gcc编译这段代码时出现如下的编译错误:

./test.cpp:19: instantiated from here

/usr/lib/gcc/x86_64-redhat-linux6E/4.4.7/../../../../include/c++/4.4.7/ext/new_allocator.h:95: error: invalid conversion from ‘const void*’ to ‘void*’

/usr/lib/gcc/x86_64-redhat-linux6E/4.4.7/../../../../include/c++/4.4.7/ext/new_allocator.h:95: error: initializing argument 1 of ‘void operator delete(void*)’

In file included from /usr/lib/gcc/x86_64-redhat-linux6E/4.4.7/../../../../include/c++/4.4.7/bits/char_traits.h:41,

from /usr/lib/gcc/x86_64-redhat-linux6E/4.4.7/../../../../include/c++/4.4.7/ios:41,

from /usr/lib/gcc/x86_64-redhat-linux6E/4.4.7/../../../../include/c++/4.4.7/ostream:40,

from /usr/lib/gcc/x86_64-redhat-linux6E/4.4.7/../../../../include/c++/4.4.7/iostream:40,

from ./test.h:12,

from ./test.cpp:9:

/usr/lib/gcc/x86_64-redhat-linux6E/4.4.7/../../../../include/c++/4.4.7/bits/stl_algobase.h: In static member function ‘static _BI2 std::__copy_move_backward::__copy_move_b(_BI1, _BI1, _BI2) [with _BI1 = const std::shared_ptr*, _BI2 = const std::shared_ptr*]’:

/usr/lib/gcc/x86_64-redhat-linux6E/4.4.7/../../../../include/c++/4.4.7/bits/stl_algobase.h:595: instantiated from ‘_BI2 std::__copy_move_backward_a(_BI1, _BI1, _BI2) [with bool _IsMove = true, _BI1 = const std::shared_ptr*, _BI2 = const std::shared_ptr*]’

/usr/lib/gcc/x86_64-redhat-linux6E/4.4.7/../../../../include/c++/4.4.7/bits/stl_algobase.h:605: instantiated from ‘_BI2 std::__copy_move_backward_a2(_BI1, _BI1, _BI2) [with bool _IsMove = true, _BI1 = const std::shared_ptr*, _BI2 = const std::shared_ptr*]’

/usr/lib/gcc/x86_64-redhat-linux6E/4.4.7/../../../../include/c++/4.4.7/bits/stl_algobase.h:676: instantiated from ‘_BI2 std::move_backward(_BI1, _BI1, _BI2) [with _BI1 = const std::shared_ptr*, _BI2 = const std::shared_ptr*]’

/usr/lib/gcc/x86_64-redhat-linux6E/4.4.7/../../../../include/c++/4.4.7/bits/vector.tcc:308: instantiated from ‘void std::vector<_tp _alloc>::_M_insert_aux(__gnu_cxx::__normal_iterator::_Tp_alloc_type::pointer, std::vector<_tp _alloc> >, _Args&& ...) [with _Args = const std::shared_ptr&, _Tp = const std::shared_ptr, _Alloc = std::allocator >]’

/usr/lib/gcc/x86_64-redhat-linux6E/4.4.7/../../../../include/c++/4.4.7/bits/stl_vector.h:741: instantiated from ‘void std::vector<_tp _alloc>::push_back(const _Tp&) [with _Tp = const std::shared_ptr, _Alloc = std::allocator >]’

可以看到原来是stl_vector与const std::shared_ptr

结合使用出现问题,于是我这样定义:

std::vector<:shared_ptr> > pointer_arrays;

std::vector<:shared_ptr> >::const_iterator pointer_Iter;再次编译,通过了。

这是因为:

shared_ptr

T> p; is similar to const

T * p; (or, equivalently, T

const * p;), that is, the pointed object is const whereas const

shared_ptr p; is similar to T*

const p; which means that p is const.

In summary:

shared_ptrp;--->T*p;:nothingisconstconstshared_ptrp;--->T*constp;:pisconstshared_ptrp;--->constT*p;<=>Tconst*p;:*pisconstconstshared_ptrp;--->constT*constp;<=>Tconst*constp;:pand*p areconst.

The same holds for weak_ptr and unique_ptr.

这里constshared_ptrp;p是const的,所以指针是固定的,但指针指向的内容是可以变的。而在vector的allocator函数不能将const

void指针变为void指针,所以出现了编译错误。

原文:http://blog.csdn.net/dqjyong/article/details/23696695

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值