C++ std::shared_ptr usage and information - c++

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?

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)

You were also asking for references or literature...
I found 3 articles that may help:
An article on Smart Pointers, which is an overview good for a general understanding.
An actual reference for std::shared_ptr.
A great tutorial discussing every method of TR1 shared_ptr along with sample code.
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() );

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() );

Related

No way to compile smart pointer with mingw-w64

I am using lasted mingw w64 compiler (from April 2014, with g++ 4.9.0), and I am unable to compile smart pointers (including <memory> header ), either with -std=c++0x or gnu0x. I get always same error:
error: 'shared_ptr' in namespace 'std' does not name a template type
I was told that lastest gcc was able to compile that. But this don't seem to be my case. I am missing anything?
Remove the -ansi compiler flag. For C++, it is equivalent to -std=c++98.
It comes after -std=c++11 and is overriding it. std::shared_ptr did not exist in C++98

Can't access std::move(...) C++ 11

I want to use the std::move function in <utility>. So I put an include for this at the top of my code along with all the others. However, when I use the move function eclipse underlines it as red and it won't compile. I know I am using c++ 11 since I can declare move constructors however, this won't work. I am using GCC to compile and I used the -std=c++11 option. I also put this in my linker. Before that #include <utility> would not show up. Do I need to include something else?
Here is the basic prolbem. std::move(...) does not seem to be defined.
#include <utility>
#include <vector>
int main()
{
std::vector<int> v1;
std::vector<int> v2;
v1 = std::move(v2); // Function move could not be resolved.
return 0;
}
Also here are the options I have set on my compiler -O3 -g3 -Wall -c -fmessage-length=0 -std=c++11
Your code is OK. But you should update your gcc to newest version. Gcc 4.7.2 don't have everything implemented from c++11 standard.
Are you sure is not defined? Perhaps your error is at linking time cause the linker cannot find the library?
Whether the case, you have to add library paths to your eclipse project settings as well as include paths.
I've compiled your example and after executing ldd on the generated binary I can see there are linking dependences, I hope this help:

the program does not recognize smart pointer

Regarding the use of shared_pointers in a c++ program, I have declared the #include <memory>
and then called upon the following std::shared_ptr<int>(new int(5)) it refuses to compile saying that
error: 'shared_ptr' is not a member of 'std'
or alternatively
Symbol 'shared_ptr' could not be resolved
Can anyone tell me why this is happening? from what i read, shared point should be defined in std or boost libraries and i have tried them both
std::shared is a C++11 feature.
You must have a compiler supporting this.
For example with GCC you should add -std=c++11 or -std=gnu++11.
If you don't have C++11, you can also use boost::shared_ptr.

array not a member of namespace::std

I am using g++ version 4.5.2 .
Although I have include array header file
#include <array> it does not complain about the existence of the header file.
But when I do std::array<char , 16>,
the exception is thrown when I try to compile with g++
"array not a member of namespace std"
I searched on the net and saw a suggestion somewhere to use std::tr1::array<>,
but that did not help either ?
Does someone know what the problem is ?
You need to compile with GCC 4.6 and later to have C++11 features and you need to set the right flag to -std=C++0x or in GCC 4.7 -std=C++11

bind is not a member of std

I am using netbeans 7.2.1 with minwg compiler. I am getting the following error messages when trying to build the application:
error: 'function' in namespace 'std' does not name a type
error: 'bind' is not a member of 'std'
although I included functional.h in the begining of the file, and I am using 'function' and 'bind' in the form of: std::function and std::bind
Where is the problem? Is it in the compiler or there is something missing? I remember that I compiled and ran the same application successfully on visual studio 2010.
It is not functional.h, it is just functional.
#include <functional> //without .h
Note that std::function and std::bind come with C++11 only. So you might have to upgrade your compiler in case you have not done yet.
Also, compile your code with -std=c++11 option:
$ g++ -std=c++11 file.cpp
That should work if you've upgraded your compiler. If your compiler is a bit old, you can also try -std=c++0x instead.
You need to include the header functional. It is available in C++11. If you are still having problems, then your compiler may not support C++11 yet. Try upgrading.
You can also use boost::bind:
#include <boost/bind.hpp>