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
Related
I am having a problem with modifying a string. I appreciate your assistance; thank you!
struct Drawings::menues
{
std::vector<std::string> variable;
} Menue[numMenues];
Menue[1].variable.at(0).pop_back();
the above code gives me the following error
error: 'class std::basic_string<char>' has no member named 'pop_back'|
I am using codeblocks 12.11 and am using the gnu gcc compiler and the C++11 flag under compiler settings fixed my problem.
thank you all!
pop_back was introduced in C++11. Make sure your compiler supports it and compile with -std=c++11 (or -std=c++0x only if -std=c++11 is not supported on your version of the compiler).
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
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>
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() );
So on my VS2010 I can compile code like :
boost::shared_ptr<boost::thread> internal_thread;
boost::packaged_task<void> internal_task_w(boost::bind(&thread_pool::internal_run, this, internal_thread));
internal_thread = boost::shared_ptr<boost::thread>( new boost::thread(std::move(internal_task_w)));
first 2 lines are ok with boost 1.47.0 and linux... but on std::move it gives error: ‘move’ is not a member of ‘std’. On VS2010 it does not require any special header. So I wonder which header it requires on linux and is it in its STD anyway? If not how to get around it with boost or something?
To get g++ into C++11 (or C++0x) mode, you have to add the command-line parameter -std=c++0x on versions <= 4.6, nowadays you can also use -std=c++11.
You are using the most recent Visual Studio, but not the most recent GCC. The std::move capability is available in the most recent GCC. It is a new feature of C++11.
You can't use std::move because your compiler does not support c++11.