108

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?

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.

I am working on 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.

Thanks for all the responses.

James McNellis
  • 348,265
  • 75
  • 913
  • 977
Jake
  • 11,273
  • 21
  • 90
  • 147
  • 3
    probably it will be helpful if you can state your setup correctly, like what compiler and boost version/installation path – YeenFei May 27 '10 at 03:03
  • 7
    Unless you're on C++0x, `shared_ptr` is not part of the standard. It's so common though that some will treat it as standard even though it's not in yet. – Billy ONeal May 27 '10 at 03:03
  • 4
    How do you think we should answer this question without knowing whether your environment is GCC for a 7.5bit dishwasher chip, a 128bit mainframe's proprietary compiler, or XCode's version of GCC? – sbi May 27 '10 at 06:45
  • Just to add some info about the problem I faced.. If you want to compile with c++0x standard you need to add "-std=c++0x" as argument of g++. – Mital Vora Nov 06 '11 at 03:20
  • 1
    If you're on MSVC, then you just need "#include " (for gcc, I have a CMake Find() for searching so that I can declare preprocessor definition to include either versus as first choice being tr1 over boost - note that boost is "hpp" while tr1 is ".h" - verified on Gentoo/Fedora/Debian - and of course make sure to also have #include for memory management separately) – HidekiAI Apr 29 '16 at 02:21

4 Answers4

172

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

James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • 3
    For VS 2008, did you get the "feature pack" or did you install SP1? If so then part 2 here applies to you, use the namespace `tr1`. – Kate Gregory May 27 '10 at 12:12
  • Thanks. This works like magic. I don't know why, but I was including and and while looking everywhere for the shared_ptr.. of course, i couldn't. Thanks again. – Jake May 27 '10 at 15:00
  • Having stepped away from C++ briefly I was surprised to find that (in clang v3.1) `shared_ptr` was still sitting in a `tr1` namespace. Any thoughts on this? – hiwaylon Aug 08 '12 at 12:37
  • 3
    @hiwaylon: Are you compiling with `-std=c++11`? – James McNellis Aug 08 '12 at 16:23
  • @JamesMcNellis Yessir, unfortunately that caused some unhappiness with other dependencies and I was unable to continue (given time constraints). If **-std=c++11** is the trick, I can continue with confidence when I am able to return to the project. Thank you. – hiwaylon Aug 27 '12 at 15:24
  • kinda already mentioned, but `#include ` along with `std::shared_ptr` only worked for me after explicitly adding the `-std=c++11` flag to `g++` version 5.3.1 – yano Oct 13 '17 at 15:42
6

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>
Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236
YeenFei
  • 3,180
  • 18
  • 26
2

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

Sebastian Mach
  • 38,570
  • 8
  • 95
  • 130
John Dibling
  • 99,718
  • 31
  • 186
  • 324