linux使用shared ptr出現錯誤

2021-06-21 12:53:09 字數 4142 閱讀 5231

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

std::vector> pointer_arrays;

std::vectorbject

> >::const_iterator pointer_iter;

並採用如下初始化:

const std::shared_ptrobject(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> pointer_arrays;

std::vectorbject

> >::const_iterator pointer_iter;

再次編譯,通過了。

這是因為:

shared_ptrp;is similar toconst t * p;(or, equivalently,t const * p;), that is, the pointed object isconstwhereasconst shared_ptrp;is similar tot* const p;which means thatpisconst. in summary:

shared_ptr

<

t>p;

--->t *

p;:nothing

isconst

const

shared_ptr

<

t>p;

--->t *

constp;

:p isconst

shared_ptr

<

const

t>p;

--->

constt *

p;<=>

t const*p

;:*p

isconst

const

shared_ptr

<

const

t>p;

--->

constt *

constp;

<=>

t const

*constp;

:p and*

p are

const

.

the same holds forweak_ptrandunique_ptr.

這裡const

shared_ptr

<

t>

p;p是const的,所以指標是固定的,但指標指向的內容是可以變的。而在vector的allocator函式不能將const void指標變為void指標,所以出現了編譯錯誤。

智慧型指標 auto ptr與shared ptr

auto ptr auto ptr是當年c 標準庫中提供的一種智慧型指標。auto ptr在構造時獲取某個物件的所有權,在析構時釋放該物件。可以提高 的安全性。例如 int p new int 0 auto ptrap p auto ptr主要是解決被異常丟擲時發生資源洩露問題。注意 1 auto ...

boost的指標智慧型指標(shared ptr)

boost智慧型指標常用的函式 get 獲取原始指標 bool unique 檢測是否唯一 long use count 引用計數 void swap 交換指標 reset 將引用計數減1,停止對指標的共享,除非引用計數為0,否則不會發生刪除操作。shared ptrspi new int 乙個in...

Linux 使用技巧

1.使用虛擬控制台 登入後按alt f2鍵這時又可以看到 login 提示符,這個就是第二個虛擬控 制台.一般新安裝的linux有四個虛擬控制台,可以用alt f1 alt f4來訪問.虛擬 控制台最有用的時候是當乙個程式出錯鎖住輸入時可以切換到其他虛擬控制台登入 進入後殺掉這個出錯的程序.2.拷貝...