C++ std::shared_ptr 用法和信息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8171444/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ std::shared_ptr usage and information
提问by MWright
I am trying to use std::shared_ptr
in my code. I have seen there have been other questions on the subject, but I am still getting a compiler error. Have I got the right version of gcc and setup?
我正在尝试std::shared_ptr
在我的代码中使用。我已经看到有关该主题的其他问题,但我仍然收到编译器错误。我有正确版本的 gcc 和设置吗?
What I have done:
我做了什么:
I have tried to compile my code with both headers separately — <memory>
and <tr1/memory>
but still get the errors below in both cases.
我试图编译我与另两个头的代码-<memory>
和<tr1/memory>
,但仍然在这两种情况下得到下面的错误。
The version of gcc I am using is
我使用的 gcc 版本是
gcc --version
gcc (GCC) 4.3.2
When I include <memory>
header I use std::shared_ptr
and with the <tr1/memory>
header I use std::tr1::shared_ptr
? Is this correct?
当我包含<memory>
我使用std::shared_ptr
的<tr1/memory>
标题和我使用的标题时std::tr1::shared_ptr
?这样对吗?
I have set up the shared_ptr as follows:
我已经设置了 shared_ptr 如下:
std::shared_ptr<A*> ptr_A = shared_ptr( new A() );
The error I get is as follows:
我得到的错误如下:
src/WH.cxx:156: error: 'shared_ptr' is not a member of 'std'
src/WH.cxx:162: error: 'shared_ptr' was not declared in this scope
when I try the <tr1/memory>
header
当我尝试<tr1/memory>
标题时
src/WH.cxx:156: error: 'std::tr1' has not been declared
src/WH.cxx:162: error: 'shared_ptr' was not declared in this scope
Looks like I am not including something correctly. Any ideas?
看起来我没有正确包含某些内容。有任何想法吗?
I know the boost library has shared_ptr
but these libraries are not an option for me at the moment.
我知道 boost 库有,shared_ptr
但目前这些库对我来说不是一个选择。
EDIT: Just to add, my compiler options are as follows: g++ -O3 -g3 -m32 -fPIC -Wno-deprecated -pthread -m32 Am I missing anything?
编辑:补充一下,我的编译器选项如下: g++ -O3 -g3 -m32 -fPIC -Wno-deprecated -pthread -m32 我错过了什么吗?
P.S. Is there any useful literature on the std smart pointers?
PS 是否有关于 std 智能指针的有用文献?
回答by Alok Save
std::tr1::shared_ptr
is part of the TR1 additions to the C++ STL.
With GCC, it is available either through #include <tr1/memory>
(GCC 4.1) or #include <memory>
(GCC 4.3)
std::tr1::shared_ptr
是 C++ STL 添加的 TR1 的一部分。
使用 GCC,它可以通过#include <tr1/memory>
(GCC 4.1) 或#include <memory>
(GCC 4.3) 获得
回答by Carsten Greiner
You were also asking for references or literature...
您还要求参考文献或文献...
I found 3 articles that may help:
我找到了 3 篇可能有帮助的文章:
- An article on Smart Pointers, which is an overview good for a general understanding.
- An actual referencefor
std::shared_ptr
. - A great tutorialdiscussing every method of TR1
shared_ptr
along with sample code.
- 一篇关于智能指针的文章,这是一个有助于一般理解的概述。
- 一个实际的参考了
std::shared_ptr
。 - 一个很棒的教程,讨论了 TR1 的每种方法
shared_ptr
以及示例代码。
Also a comment on your code example:
还有对您的代码示例的评论:
std::shared_ptr<A*> ptr_A = shared_ptr( new A() );
The template argument should be A instead of A* :
模板参数应该是 A 而不是 A* :
std::shared_ptr<A> ptr_A = shared_ptr( new A() );
回答by murrekatt
If you don't have shared_ptr
in std you can use it from boost.
如果您shared_ptr
在 std 中没有,则可以从 boost 中使用它。
#include <boost/shared_ptr.hpp>
boost::shared_ptr<A> ptr_A( new A() );