I would like to generate numbers with normal_distribution in c++11. Using Qt 4.8 MinGW. I have added the next line
QMAKE_CXXFLAGS += -std=c++0x
to my .pro file
and then get next errors:
'swprintf::' has not been declared
'vswprintf::' has not been declared
TR1 really should be avoid when using C++11 as it was designed to meet the limitations of the previous standard. (also everything that was considered useful went on and became integrated into the standard.)
Luckily there is a comprehensive random number generation library in C++11 with normal distribution.
See here:
http://en.cppreference.com/w/cpp/numeric/random
#include <random>
:::
std::random_device rd;
std::normal_distribution<double> dist(0,99);
std::mt19937 engine(rd());
double a=dist(engine);
The exact error your getting look as though the particular implementation of TR1 isn't very good anyway. (missing include or missing namespace prefix).
Related
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?
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.
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.
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. ;-)
I hate to recommend this but in your case (old untouchable machine) I can offer a suggestion. The tr1 version of the random library should be available for g++-4.4:
#include <tr1/random>
int main(int argc, char *argv[]){
std::tr1::default_random_engine generator;
return 0;
}
There have been improvements in the std version relative to tr1 version but you should be able to use most <random> features.
You don't even need C++0x.
I'm trying to compile a program I wrote in linux on Xcode and for my random functions I used:
std::random_device seed_device;
std::default_random_engine engine(seed_device());
But default_random_engine won't compile on Xcode. Is there a different version I should use?
You need to #include <random>.
In general, when the compiler complains about "no type named function_name in namespace std", you have to check that:
You have included the right header. You can look up the name of the right header on any decent C++ reference.
Your standard library supports C++1x (*) (or, at least, the feature you want).
Your compiler supports C++1x (*) (or, at least, the feature you want), and the right flags are used to enable it, if required.
(*) The x stands for the version of the C++ standard that includes the feature you want.
While compiling the above code
this is the error which i am getting.
#include <random>
#include <iostream>
int main()
{
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
}
In file included from /usr/include/c++/4.8/random:35:0,
from fv.cpp:1:
/usr/include/c++/4.8/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
My gcc version is g++ (Ubuntu 4.8.2-19ubuntu1) 4.8.2
Please help me
This will compile fine after adding the -std=c++11 flag to your compilation command. This flag is needed to make gcc support c++11 features such as <random>.
If you compile in an terminal, just add the flag somewhere. If you use an IDE, you might have to change some option, but that does (of course) depend on your specific IDE.
I would like to write a "portable" C++ library in Clang. "Portable" means that I detect (in C preprocessor) what C++ features are available in the compilation environment and use these features or provide my workarounds. This is similar to what Boost libraries are doing.
However, the presence of some features depends not on the language, but on the Standard Library implementation. In particular I am interested in:
type traits (which of them are available and with what spelling)
if initializer_list being constexpr.
I find this problematic because Clang by default does not use its own Standard Library implementation: it uses libstdc++. While Clang has predefined preprocessor macros __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__, they are hardcoded to values 4, 2, 1 respectively, and they tell me little about the available libstdc++ features.
How can I check in Clang preprocessor what version of libstdc++ it is using?
Clang does come with its own standard library implementation, it's called libc++. You can use it by adding -stdlib=libc++ to your compile command.
That being said, there are various ways to check Clang/libstdc++ C++ support:
Clang has the __has_feature macro (and friends) that can be used to detect language features and language extenstions.
Libstdc++ has its own version macros, see the documentation. You'll need to include a libstdc++ header to get these defined though.
GCC has its version macros which you already discovered, but those would need to be manually compared to the documentation.
And also, this took me 2 minutes of googling.
This is what I think would help. It prints the value of the _LIBCPP_VERSION macro:
#include <iostream>
#include <string>
using namespace std;
int main(int argc, const char * argv[])
{
cout<<"Value = "<<_LIBCPP_VERSION<<endl;
return 0;
}
Compile it again the version of clang you want the info for.
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.
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.
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