std random functions not working - Qt MinGw

2019-08-30 10:25发布

问题:

I am using QtCreator 2.4.1, with QtSdk 4.8.1 and MinGw 4.7.2

I am trying to use the (c++11) random library, but so far I have been unsuccesful. Take the following sample code:

#include <random>
...
std::default_random_engine generator;
std::uniform_int_distribution<int> distribution(1,6);
int dice_roll = distribution(generator);  // generates number in the range 1..6

The compiler complains:

error: 'default_random_engine' is not a member of 'std'

error: 'uniform_int_distribution' is not a member of 'std'

I've got the -std=c++0x flag in my .pro file. All other STL functionality seems to work properly, so I'm puzzled! I'd be grateful if someone could give me a hand with this.

回答1:

Well, silly problem, but might happen to others, so here we go:

For some reason my code was being compiled with MinGW 4.4, the version that shipped by default with my Qt Creator. To bring it back to MinGw 4.7.2, which I already had installed in my computer, I clicked on the 'Projects' tab on the left of the QCreator screen, then selected the correct tool chain.

Thanks loads to Joachim Pileborg for pointing me in the right direction.



回答2:

I ran into the same problem, and was unable to change the compiler.

I replaced std::rand() with qrand()

http://qt-project.org/doc/qt-4.8/qtglobal.html#qrand