C++ shared_ptr 在哪里?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2918202/
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
Where is shared_ptr?
提问by Jake
I am so frustrated right now after several hours trying to find where shared_ptr is located. None of the examples I see show complete code to include the headers for shared_ptr
(and working). Simply stating std
, tr1
and <memory>
is not helping at all! I have downloaded boosts and all but still it doesn't show up! Can someone help me by telling exactly where to find it?
在试图找到 shared_ptr 的位置几个小时后,我现在非常沮丧。我看到的示例都没有显示完整的代码来包含shared_ptr
(和工作)的标题。简单地说std
,tr1
并<memory>
没有帮助!我已经下载了提升,但仍然没有出现!有人可以通过确切地告诉我在哪里可以找到它来帮助我吗?
Thanks for letting me vent my frustrations!
谢谢你让我发泄我的不满!
EDIT: I see my title has been changed. Sorry about that. So... it was also because it was not clear to me that shared_ptr is "C++ version dependant" --> that's why I did not state my environment --> therefore probably why it was so difficult for me to find it.
编辑:我看到我的标题已更改。对于那个很抱歉。所以...也是因为我不清楚 shared_ptr 是“C++ 版本依赖”--> 这就是为什么我没有说明我的环境--> 因此可能是为什么我很难找到它。
I am working on MSVS2008.
我正在研究 MSVS2008。
EDIT 2: I don't know why, but I was including [memory] and [boost/tr1/memory.hpp] and [boost/tr1/tr1/memory] while looking everywhere for the shared_ptr.. of course, i couldn't.
编辑 2:我不知道为什么,但我在到处寻找 shared_ptr 时包括 [memory] 和 [boost/tr1/memory.hpp] 和 [boost/tr1/tr1/memory] .. 当然,我可以'不。
Thanks for all the responses.
感谢所有的回应。
回答by James McNellis
There are at least three places where you may find shared_ptr
:
您至少可以在三个地方找到shared_ptr
:
If your C++ implementation supports C++11 (or at least the C++11
shared_ptr
), thenstd::shared_ptr
will be defined in<memory>
.If your C++ implementation supports the C++ TR1 library extensions, then
std::tr1::shared_ptr
will likely be in<memory>
(Microsoft Visual C++) or<tr1/memory>
(g++'s libstdc++). Boost also provides a TR1 implementation that you can use.Otherwise, you can obtain the Boost libraries and use
boost::shared_ptr
, which can be found in<boost/shared_ptr.hpp>
.
如果您的 C++ 实现支持 C++11(或至少 C++11
shared_ptr
),则将std::shared_ptr
在<memory>
.如果您的 C++ 实现支持 C++ TR1 库扩展,那么
std::tr1::shared_ptr
很可能在<memory>
(Microsoft Visual C++) 或<tr1/memory>
(g++'s libstdc++) 中。Boost 还提供了一个可以使用的 TR1 实现。否则,您可以获取 Boost 库并使用
boost::shared_ptr
,可以在<boost/shared_ptr.hpp>
.
回答by Firas Assaad
回答by YeenFei
for VS2008 with feature pack update, shared_ptr can be found under namespace std::tr1.
对于带有功能包更新的 VS2008,可以在命名空间 std::tr1 下找到 shared_ptr。
std::tr1::shared_ptr<int> MyIntSmartPtr = new int;
of
的
if you had boost installation path (for example @ C:\Program Files\Boost\boost_1_40_0
) added to your IDE settings:
如果您在C:\Program Files\Boost\boost_1_40_0
IDE 设置中添加了 boost 安装路径(例如 @ ):
#include <boost/shared_ptr.hpp>
回答by John Dibling
If your'e looking bor boost's shared_ptr, you could have easily found the answer by googling shared_ptr, following the links to the docs, and pulling up a complete working example such as this.
如果您正在寻找 bor boost的shared_ptr,您可以通过谷歌搜索 shared_ptr轻松找到答案,按照文档链接,并提取一个完整的工作示例,例如this。
In any case, here is a minimalistic complete working example for you which I just hacked up:
无论如何,这是我刚刚修改的一个简约的完整工作示例:
#include <boost/shared_ptr.hpp>
struct MyGizmo
{
int n_;
};
int main()
{
boost::shared_ptr<MyGizmo> p(new MyGizmo);
return 0;
}
In order for the #include
to find the header, the libraries obviously need to be in the search path. In MSVC, you set this in Project Settings>Configuration Properties>C/C++>Additional Include Directories. In my case, this is set to C:\Program Files (x86)\boost\boost_1_42
为了#include
找到标题,库显然需要在搜索路径中。在 MSVC 中,您可以在项目设置>配置属性>C/C++>附加包含目录中进行设置。在我的情况下,这被设置为C:\Program Files (x86)\boost\boost_1_42