c++ 0x: uniform_real_distribution不是std的成员

C++0x: uniform_real_distribution is not a member of std

本文关键字:不是 std 成员 distribution real 0x uniform c++      更新时间:2023-10-16

我正在尝试使用c++0x编译我的第一段代码。我很擅长c++,但是这个问题难倒了我。以下代码将无法编译:

#include <iostream>
#include <random>
int main()
{
   unsigned     seed = 12345;
   std::mt19937 generator(seed);
   std::uniform_real_distribution<double> distro1(0.0, 1.0);
   for (unsigned int i = 0; i < 25; i++)
   {
      std::cout << "Help" << std::endl;
      std::cout << "Value  ==  " << distro1(generator) << std::endl;
   }
}

我用下面的代码编译:

g++ -std=c++0x myFile.cpp or with g++ -std=gnu++0x myFile.cpp

我得到的错误是:

myFile.cpp: In function ‘int main()’:
myFile.cpp:11: error: ‘uniform_real_distribution’ is not a member of ‘std’
myFile.cpp:11: error: expected primary-expression before ‘double’
myFile.cpp:11: error: expected ‘;’ before ‘double’
myFile.cpp:15: error: ‘distro1’ was not declared in this scope

g++是:g++ --versiong++ (Debian 4.4.5-8) 4.4.5

奇怪的是,它编译过去的mt19937实例化,如果我只是调用mt19937::operator(),我得到预期的输出。此外,如果我将发行版切换为normal_distribution,它会编译并输出"帮助"行,但不会做任何其他事情。可以看到该进程通过ps aux运行,但没有输出。

我错过了什么愚蠢的东西吗?我基本上是从cplusplus.com复制代码来显示一个mwe。谢谢你的帮助

您必须升级到更高版本的编译器(您的程序在GCC 4.7.2上编译得很好)。GCC 4.4.5附带的标准库的实现显然不(完全)与c++ 11兼容。