C++ std::shared_ptr usage and information
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 —
<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
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?
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)
C (programming language), C programming is a general-purpose, procedural, imperative computer programming language developed in 1972 by Dennis M. Ritchie at the Bell Telephone C programming is a general-purpose, procedural, imperative computer programming language developed in 1972 by Dennis M. Ritchie at the Bell Telephone Laboratories to develop the UNIX operating system. C is the most widely used computer language.
C Programming Tutorial for Beginners, C Language - Overview - C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system The C programming language is a computer programming language that was developed to do system programming for the operating system UNIX and is an imperative programming language. C was developed in the early 1970s by Ken Thompson and Dennis Ritchie at Bell Labs. It is a procedural language, which means that people can write their programs as a
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() );
"C" Programming Language: Brian Kernighan, C is a powerful general-purpose programming language. It can be used to develop software like operating systems, databases, compilers, and so on. C is a procedural programming language. It was initially developed by Dennis Ritchie as a system programming language to write operating system. The main features of C language include low-level access to memory, simple set of keywords, and clean style, these features make C language suitable for system programming like operating system or compiler development.
Learn C, The best site for C and C++ programming. Popular, beginner-friendly C and C++ tutorials to help you become an expert! Discover historical prices for C stock on Yahoo Finance. View daily, weekly or monthly format back to when Citigroup, Inc. stock was issued.
C Tutorial, C is a powerful systems programming language. Learn C with our popular C tutorial, which will take you from the very basics of C all the way through sophisticated Hello World using C++. Just to give you a little excitement about C++ programming, I'm going to give you a small conventional C++ Hello World program, You can try it using Demo link. C++ is a super set of C programming with additional implementation of object-oriented concepts.
C Language - Overview, C is a computer programming language. That means that you can use C to create lists of instructions for a computer to follow. C is one of thousands of Learn C# programming - for beginning developers, developers new to C#, and experienced C# / .NET developers
- 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
- Depending on your version of GCC,
std::shared_ptrmay not exist, or may require you to specify
-std=c++0xas a compiler option.
- 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.
- Requests for tutorials/literature is outside the scope of this site as defined in the FAQ.
- @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.
- In GCC it depends on the presence of the
-std=c++0xcompiler option, too. (Or
-std=c++11from 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++0xisn'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