18

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?

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.

The version of gcc I am using is

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?

I have set up the shared_ptr as follows:

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

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.

EDIT: Just to add, my compiler options are as follows: g++ -O3 -g3 -m32 -fPIC -Wno-deprecated -pthread -m32 Am I missing anything?

P.S. Is there any useful literature on the std smart pointers?

Timo Geusch
  • 24,095
  • 5
  • 52
  • 70
MWright
  • 1,681
  • 3
  • 19
  • 31
  • You'd expect the documentation to include two things, but it doesn't: which header file to include, and which version of gcc first introduced it. http://gcc.gnu.org/onlinedocs/libstdc++/manual/shared_ptr.html – Mark Ransom Nov 17 '11 at 17:24
  • Depending on your version of GCC, `std::shared_ptr` may not exist, or may require you to specify `-std=c++0x` as a compiler option. – Mike Seymour Nov 17 '11 at 17:29
  • 1
    First, gcc 4.3 is very old. As of when you wrote your question, gcc 4.6 is out. Second, I believe shared_ptr is a C++11 feature, so even once you have the correct version, you will probably need to use -std=c++0x to use it. – wjl Nov 17 '11 at 17:32
  • Requests for tutorials/literature is outside the scope of this site as defined in the [FAQ](http://stackoverflow.com/faq). – Keith Pinson Dec 19 '12 at 23:08
  • @wjl Some of us can't update compilers easily. Embedded systems often come in a collection of things, including compilers and hardware packages. Updating compiler versions can require physically changing the hardware, and that can be _very_ expensive. – James Moore Aug 09 '18 at 19:20

3 Answers3

24

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)

Alok Save
  • 202,538
  • 53
  • 430
  • 533
10

You were also asking for references or literature...

I found 3 articles that may help:

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* :

std::shared_ptr<A> ptr_A = shared_ptr( new A() ); 
Jared Harley
  • 8,219
  • 4
  • 39
  • 48
Carsten Greiner
  • 2,928
  • 2
  • 16
  • 20
  • Good references, even though technically that is not [what this site is for](http://stackoverflow.com/faq). – Keith Pinson Dec 19 '12 at 23:10
  • 1
    true, although he was clever enough to ask at least one stackoverflow adequate question. And honestly, when i see references from well reputated people, I usually follow and have a look - and this helps alot in the long run. cheers and merry Xmas – Carsten Greiner Dec 21 '12 at 18:46
6

If you don't have shared_ptr in std you can use it from boost.

#include <boost/shared_ptr.hpp>

boost::shared_ptr<A> ptr_A( new A() );
ronag
  • 49,529
  • 25
  • 126
  • 221
murrekatt
  • 5,961
  • 5
  • 39
  • 63