c++ - xutility 错误(<chrono> <random> C++ 标准库)

标签 c++ random visual-studio-2013 c++-chrono

我在使用标准和 Microsoft Visual Studio 2013 编写简单的随机数生成器时遇到问题。当我尝试编译时,它会抛出一堆与 xutility 文件相关的错误。当我尝试使用 BOOST 库来完成同样的事情时,我抛出了一堆类似的错误,所以这可能是 MSVC 的问题。

#include <iostream>
#include <chrono>
#include <random>

using std::cout;
using std::endl;

auto seed = std::chrono::high_resolution_clock::now();

int main()
{
    std::uniform_int_distribution<> randNum(1, 6);

    cout << randNum(seed) << endl;

    return 0;
}

这是它抛出的错误:

错误 1 ​​错误 C2039:“result_type”:不是“std::chrono::time_point”的成员 e:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 3256

错误 2 错误 C2146:语法错误:缺少“;”在标识符“_Ty1”之前 e:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 3256

错误 3 错误 C4430:缺少类型说明符 - 假定为 int。注意:C++不支持default-int e:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 3256

错误 4 错误 C2065:“_Ty1”:未声明的标识符 e:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 3258

错误 5 error C2070: 'unknown-type': illegal sizeof operand e:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 3258

错误 6 error C2065: '_Ty1' : undeclared identifier e:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 3259

错误 7 错误 C2923:“std::_If”:“_Ty1”不是参数“_Ty2”的有效模板类型参数 e:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 3259

错误 8 错误 C2955:“std::_If”:使用类模板需要模板参数列表 e:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 3259

这是 xutility 中似乎引起问题的代码部分:

    // TEMPLATE CLASS _Rng_from_urng
template<class _Diff, class _Urng>
    class _Rng_from_urng
    {   // wrap a URNG as an RNG
public:
    typedef typename make_unsigned<_Diff>::type _Ty0;
    typedef typename _Urng::result_type _Ty1;

    typedef typename _If<sizeof (_Ty1) < sizeof (_Ty0),
        _Ty0, _Ty1>::type _Udiff;

此后该类还有很多代码,但错误发生在最后两行。

顺便说一句,我花了几个小时查询不同的搜索以尝试找到答案,然后再发布到这里,但没有可用的结果,尽管这很可能是我的一个缺陷。

谢谢!

在 Sebastion 的帮助下,所有以前的错误都已解决,但是产生了一个新错误:

修改后的代码:

    std::default_random_engine engine(seed);

    cout << randNum(engine) << endl;

错误:

错误 1 ​​error C2039: 'generate' : is not a member of 'std::chrono::time_point' e:\program files (x86)\microsoft visual studio 12.0\vc\include\random 1618

最佳答案

cout << randNum(seed) << endl;

这就是您使用发行版的方式。您需要创建一个用种子初始化的本地引擎,然后将引擎传递给分发的调用运算符(operator)。

std::default_random_engine engine(seed);
cout << randNum(engine) << endl;

有关更详细的示例,请参阅 cppreference 中的示例:

http://en.cppreference.com/w/cpp/numeric/random

关于c++ - xutility 错误(<chrono> <random> C++ 标准库),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30956915/

相关文章:

c++ - C++标准库命名不一致

matlab - 两个数字之间的随机数生成器 - MatLab

cordova - 如何使用 Visual Studio Cordova 安装 Cordova 插件?

visual-studio-2012 - 删除项目时的 Visual Studio 扩展冲突

c++ - 有没有什么方法可以在用户输入文本时读取字符?

c++ - 在C++中,如何在头文件中声明一个数据结构,而它是在源文件中定义的?

c++ - native Node.JS 模块 - 从参数解析 int[]

javascript - jquery从textarea中获取随机单词

c# - 批量生成随 secret 码

c# - 找不到类型或命名空间名称 'MySqlConnection'(是否缺少 using 指令或程序集引用?)