linux使用shared_ptr出现错误
2014-04-14 18:44
351 查看
我在不同的系统下采用下面的方式使用shared_ptr以及vector:
并采用如下初始化:
这样在mac os下面编译,通过并且获得正确的结果。但是我在centos 下的使用gcc编译这段代码时出现如下的编译错误:
可以看到原来是stl_vector与const std::shared_ptr<Object>
结合使用出现问题,于是我这样定义:
这是因为:
In summary:
The same holds for
这里const shared_ptr<T> p;p是const的,所以指针是固定的,但指针指向的内容是可以变的。而在vector的allocator函数不能将const
void指针变为void指针,所以出现了编译错误。
std::vector<const std::shared_ptr<Object> > pointer_arrays; std::vector<const std::shared_ptr<Object > >::const_iterator pointer_Iter;
并采用如下初始化:
const std::shared_ptr<WordData> 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<true, false, std::random_access_iterator_tag>::__copy_move_b(_BI1, _BI1, _BI2) [with _BI1 = const std::shared_ptr<Object>*, _BI2 = const std::shared_ptr<Object>*]’: /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<Object>*, _BI2 = const std::shared_ptr<Object>*]’ /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<Object>*, _BI2 = const std::shared_ptr<Object>*]’ /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<Object>*, _BI2 = const std::shared_ptr<Object>*]’ /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<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >, _Args&& ...) [with _Args = const std::shared_ptr<Object>&, _Tp = const std::shared_ptr<Object>, _Alloc = std::allocator<const std::shared_ptr<Object> >]’ /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<Object>, _Alloc = std::allocator<const std::shared_ptr<Object> >]’
可以看到原来是stl_vector与const std::shared_ptr<Object>
结合使用出现问题,于是我这样定义:
std::vector<std::shared_ptr<Object> > pointer_arrays; std::vector<std::shared_ptr<Object > >::const_iterator pointer_Iter;再次编译,通过了。
这是因为:
shared_ptr<const T> p;is similar to
const T * p;(or, equivalently,
T const * p;), that is, the pointed object is
constwhereas
const shared_ptr<T> p;is similar to
T* const p;which means that
pis
const.
In summary:
shared_ptr<T> p; ---> T * p; : nothing is const const shared_ptr<T> p; ---> T * const p; : p is const shared_ptr<const T> p; ---> const T * p; <=> T const * p; : *p is const const shared_ptr<const T> p; ---> const T * const p; <=> T const * const p; : p and *p are const.
The same holds for
weak_ptrand
unique_ptr.
这里const shared_ptr<T> p;p是const的,所以指针是固定的,但指针指向的内容是可以变的。而在vector的allocator函数不能将const
void指针变为void指针,所以出现了编译错误。
相关文章推荐
- mac使用xterm远程连接Linux图形界面出现错误
- android使用google map api 出现INSTALL_FAILED_MISSING_SHARED_LIBRARY 错误
- linux下使用jni实现c++调用java程序(4)DestroyJavaVM出现错误
- linux使用apt-get install 就出现jdk7 install的错误的解决方案
- linux-centos6.3 环境下使用eclipse开发rmi 出现“ObjID already in use”错误
- linux应用编程笔记(2)core dump分析内核链表使用后free出现的错误
- sudo命令:解决使用Linux命令行时出现的错误提示
- 使用虚拟机安装Linux出现错误[Errno 5] Input/Output error
- 使用kail-linux出现文件系统有错误,需要手动修复
- Linux下使用Nginx端口转发出现502错误的一种解决办法
- sudo命令:解决使用Linux命令行时出现的错误提示
- sudo命令:解决使用Linux命令行时出现的错误提示
- 为什么使用enable_shared_from_this——shared_ptr两类错误
- Linux下使用yum安装软件时出现HTTP Error 404: NOT FOUND错误解决方案
- linux3.0.1内核下V4L2接口使用zc301摄像头出现segment fault错误的解决方法
- windows上使用map network drive 与linux连接时出现错误,Error code:0x80070035 The network path was not found
- C++使用MySQL-Connector/C++连接MySQL出现LNK2019错误
- C# RSA在服务上使用出现拒绝方法错误的解决方法
- 使用Genymotion调试出现错误INSTALL_FAILED_CPU_ABI_INCOMPATIBLE解决办法
- 使用org.w3c.dom.Element的setTextContent()、getTextContent()方法时出现编译错误