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

Errors with xutility (<chrono> <random> C++ standard libraries)

我在使用标准和 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 ​​error C2039: 'result_type' : 不是 'std::chrono::time_point' e:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 3256[ 的成员=15=]

错误 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 error C2065: '_Ty1' : 未声明的标识符 e:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 3258

错误 5 error C2070: 'unknown-type': 非法操作数 e:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 3258

错误 6 error C2065: '_Ty1': 未声明的标识符 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\xutility3259

错误 8 error C2955: 'std::_If' : 使用 class 模板需要模板参数列表 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;

class 在这之后还有很多代码,但错误发生在最后两行。

顺便说一句,我花了几个小时查询不同的搜索以尝试找到答案,然后在这里发布没有可用的结果,尽管这很容易成为我的不足。

谢谢!

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

修改后的代码:

    std::default_random_engine engine(seed);

    cout << randNum(engine) << endl;

错误:

错误 1 ​​error C2039: 'generate' : 不是 'std::chrono::time_point' e:\program files (x86)\microsoft visual studio 12.0\vc\include\random 1618[ 的成员=15=]

cout << randNum(seed) << endl;

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

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

查看 cppreference 上的示例以获得更详细的示例:

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