Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'default_random_engine' is not a member of std

Tags:

c++

gcc

c++11

I have found many questions to this topic but all problems seem to be related to not compiling with C++ 11. My code is

#include <random>
int main(int argc, char *argv[]){
    std::default_random_engine generator;
    return 0;   
}

even though I compile with

gcc -std=c++0x testmain.cpp

Giving the error that default_random_engine is not a member of std. The program is compiled on a remote machine, which I do not maintain myself but

gcc -v

yields a version of 4.4.7.

Any ideas?

like image 794
Hagadol Avatar asked Nov 02 '15 16:11

Hagadol


3 Answers

As DevSolar already stated, your gcc version is too old, to support this C++11 feature.

It was added in gcc-4.5:

Improved experimental support for the upcoming ISO C++ standard, C++0x, including:

Support for <future>, <functional>, and <random>.

Reference: https://gcc.gnu.org/gcc-4.5/changes.html

This is also reflected by the libstdc++ API Reference: https://gcc.gnu.org/onlinedocs/libstdc++/libstdc++-api-4.5/a01118.html

where you can find the following:

 typedef minstd_rand0 default_random_engine

Your code works fine for me in: gcc-5.1.0, gcc-4.9.2 and clang-3.7.0,

Also you should use the command: g++ instead of gcc so gcc links against proper c++ libraies by default.

like image 89
oo_miguel Avatar answered Nov 12 '22 12:11

oo_miguel


For others: Check if you actually include random with #include <random>. I didn't have it and some other header included it previously. Now that header got updated and I got this error and didn't find it for a while because I was checking compiler settings.

like image 6
Hakaishin Avatar answered Nov 12 '22 11:11

Hakaishin


Your problem is you're not compiling with C++11. ;-) (Sorry, could not resist.)

GCC 4.4.7 is dated March 2012. C++11 support was not yet complete in that version.

As of the time of this writing, the current version of GCC is 5.2.0... which is C++14 compliant. Time to update your compiler. ;-)

like image 1
DevSolar Avatar answered Nov 12 '22 12:11

DevSolar