Share_ptr 在哪里?

在花了几个小时试图找到 share _ ptr 的位置之后,我现在感到非常沮丧。我看到的所有示例都没有显示完整的代码来包含 shared_ptr(和工作)的头部。简单地陈述 stdtr1<memory>根本没有帮助!我已经下载了推进和所有,但仍然没有显示!谁能告诉我在哪里能找到它?

谢谢你让我发泄我的沮丧!

编辑: 看来我的头衔改了,不好意思。 所以... ... 这也是因为我不清楚 share _ ptr 是否是“ C + + 版本依赖的”—— > 这就是为什么我没有说明我的环境—— > 因此可能也是为什么我很难找到它的原因。

我正在为 MSVS2008工作。

编辑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.

谢谢你的回复。

115116 次浏览

for VS2008 with feature pack update, shared_ptr can be found under namespace std::tr1.

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:

#include <boost/shared_ptr.hpp>

There are at least three places where you may find shared_ptr:

  1. If your C++ implementation supports C++11 (or at least the C++11 shared_ptr), then std::shared_ptr will be defined in <memory>.

  2. 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.

  3. Otherwise, you can obtain the Boost libraries and use boost::shared_ptr, which can be found in <boost/shared_ptr.hpp>.

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.

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