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?

5
  • 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. gcc.gnu.org/onlinedocs/libstdc++/manual/shared_ptr.html Commented Nov 17, 2011 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. Commented Nov 17, 2011 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. Commented Nov 17, 2011 at 17:32
  • Requests for tutorials/literature is outside the scope of this site as defined in the FAQ. Commented Dec 19, 2012 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. Commented Aug 9, 2018 at 19:20

3 Answers 3

25

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)

Sign up to request clarification or add additional context in comments.

5 Comments

In GCC it depends on the presence of the -std=c++0x compiler option, too. (Or -std=c++11 from 4.7 onwards.)
Okay so here is my compiler options I am using. g++ -O3 -g3 -m32 -fPIC -Wno-deprecated -pthread -m32 . do I need to add -std=c++0x
-std=c++0x isn't needed for std::tr1::shared_ptr, is it?
Looks Like it is needed Fred. Well for my set up. Thanks Again!
With C++11 it is now std::shared_ptr
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() ); 

2 Comments

Good references, even though technically that is not what this site is for.
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
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() );

4 Comments

Should be shared_ptr, not shred_ptr. (I know -- the error is in the question too.)
sorry :) was a bit quick here. Wanted to just point out that there shouldn't be any A* in the template.
@murrekatt: The OP has said "I know the boost library has shared_ptr but these libraries are not an option for me at the moment :-(".
Shouldn't it be boost::shared_ptr<A> ptr_A( new A ); (no parents after A)?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.