the program does not recognize smart pointer - c++

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.

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

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>

error: strstream.h: No such file or directory

I am trying to run an old C++ code in Linux (Redhat). I am using gcc version 4.1.2.
I got the following error:
error: strstream.h: No such file or directory
/trnuser1/rmtrain/DevelopmentEnv/Generic/CoreObjects/GCVTransformationServices.cpp:41: error: âostrstreamâ was not declared in this scope
/trnuser1/rmtrain/DevelopmentEnv/Generic/CoreObjects/GCVTransformationServices.cpp:41: error: expected `;' before âstrDestXMLâ
/trnuser1/rmtrain/DevelopmentEnv/Generic/CoreObjects/GCVTransformationServices.cpp:62: error: âstrDestXMLâ was not declared in this scope
This code was running fine under Solaris with gcc version 2.95. The line pointed to by the error contains the following statement:
ostrstream strDestXML;
How do I solve this?
You can #include <strstream> (note absence of the '.h' suffix).
But if you want to properly port the code to modern C++, you should consider changing this to #include <sstream> and std::ostringstream strDestXML; as suggested in the comment.
Standard C++ headers do not have extension.
#include <sstream>
Standard classes are contained in std namespace:
std::ostringstream strDestXML;
Finally, strstream is deprecated; use stringstream instead - that's why I used it here.
And, just a note about GCC version - 4.1.2 is old, no matter what - use something newer.
The modern name for this include is <strstream>. (Although it's formally deprecated, it's still required.) The classes it defines are in namespace std, and have slightly different semantics than the classical iostream, so you may have to do a little bit of modification later anyway. (Depending on how it is being used, it might make sense to change to <sstream>, replacing [io]strstream with std::[io]stringstream.)

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

Cygwin gcc compiled fails in IDE complaining about 'exit' undeclared

When I compile a program using just
gcc code.c
There are no messages, and an output file is generated successfully. The outputted file works. However, when I try to the same cygwin installation's gcc compiler in an IDE (I've tried Netbeans and Dev-C++), I get the following errors
main.cpp:27: error: `exit' undeclared (first use this function)
main.cpp:27: error: (Each undeclared identifier is reported only once for each function it appears in.)
main.cpp:77: error: `write' undeclared (first use this function)
main.cpp:78: error: `close' undeclared (first use this function)
I don't see what's different. Why does it not compile?
OK, the issue was that in the IDE, the file had a .cpp extension, whereas when I was compiling from a terminal, it had a .c extension. So, my new question is why does it not compile when it's treated as a c++ file. Isn't C a subset of C++?
C++ is stricter then C. Where C allows you to call a function without a prototype, C++ does not allow this.
To solve the problem, you want to add:
#include <stdlib.h>
Also, when compiling at the command line. Make sure to use the -Wall flag so you'll get important warnings:
gcc -Wall code.c
The IDE is using fussier options to the compiler. You need to include some headers:
#include <stdlib.h> // exit()
#include <unistd.h> // close(), write()
The default options allow almost anything that might be C to compile. By the looks of it, the IDE sets '-Wmissing-prototypes' as one of the compiler options.
If you compile code with a C++ compiler, you must ensure that all functions are declared before use. C is sloppier (or can be sloppier) about that - it is recommended practice to ensure all functions are declared before being defined or referenced, but it is not mandatory. In C++ it is not optional.
There is a subset of C that is also a subset of C++; there are bits of C that are not C++, and there are many bits of C++ that are not C. In particular, an arbitrary C program is not, in general, a C++ program. For example, a C program may not declare 'exit()' and yet it can both use it and still compile. A C++ program must declare 'exit()' before it can user it and compile.
You will have to use g++ for compiling .cpp files.
One possible reason may be that the IDE is unable to access the include files, the cygwin gcc compiler may be expecting it in /usr/include(not sure), and the dev-cpp may not be able to access it.