Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/133.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 无法使用Visual C+中的默认“随机”引擎字段创建类+;2010_C++_Visual C++_Random - Fatal编程技术网

C++ 无法使用Visual C+中的默认“随机”引擎字段创建类+;2010

C++ 无法使用Visual C+中的默认“随机”引擎字段创建类+;2010,c++,visual-c++,random,C++,Visual C++,Random,我尝试创建一个具有默认\u random\u引擎私有字段的类。但是,这个类不编译。我的简单控制台程序代码如下: // RngTest.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <ctime> #include <random> using namespace std; class MyClass { private: d

我尝试创建一个具有默认\u random\u引擎私有字段的类。但是,这个类不编译。我的简单控制台程序代码如下:

// RngTest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <ctime>
#include <random>

using namespace std;

class MyClass
{
private:
    default_random_engine Rng;
public:
    MyClass(void)
        : Rng(time(NULL))
    {
    }
    ~MyClass(void)
    {
    }
    void Seed(unsigned int seed)
    {
        Rng.seed(seed);
    }
};

int _tmain(int argc, _TCHAR* argv[])
{
    MyClass rng;
    rng.Seed(100);
    return 0;
}
//RngTest.cpp:定义控制台应用程序的入口点。
//
#包括“stdafx.h”
#包括
#包括
使用名称空间std;
类MyClass
{
私人:
默认\u随机\u引擎名称;
公众:
MyClass(无效)
:Rng(时间(空))
{
}
~MyClass(无效)
{
}
无效种子(无符号整数种子)
{
种子(种子);
}
};
int _tmain(int argc,_TCHAR*argv[]
{
MyClass rng;
种子(100);
返回0;
}
在Visual Studio 2010(静态标准库,无MFC/ATL,控制台项目)中,我看到以下编译错误:

c:\program files(x86)\microsoft visual studio 10.0\vc\include\random(1604):错误C2064:术语不计算为具有0个参数的函数 1>
c:\users\vitaliy\documents\visual studio 2010\projects\rngtest\rngtest\rngtest.cpp(25):请参阅正在编译的函数模板实例化“void std::tr1::mersenne_twister::seed(_Gen&,bool)”的参考


这种编译失败的原因可能是什么?在Visual C++ 2010中有没有使用Debug TrimeRoad引擎私有字段的方法?< /P> < P>我花了大约4个小时试图解决这个问题。万一有人碰到这个

默认的\u随机\u生成器将只接受类型为unsigned long的种子。Int,unsigned Int,long-short-byte-dword不会剪切它。它必须是无符号整数

还请注意,您不能在0之前进行种子设定,您将得到一个运行时错误。下面是我来之不易的编译代码

unsigned long seed = pblock->GetInt(p_seed);
// We cannot seed with 0, so increment by 1 to avoid the error
seed++;
std::default_random_engine generator(seed);

他也有类似的问题。在Visual C++10中,种子必须使用无符号类型

问题是VS2010的C++11实现不完整。是的,你说得对,我看到他们没有实现从整数开始的种子设定,但是,他们实现了从seed_seq开始的种子设定,所以需要使用seed_seq参数而不是unsigned int进行种子设定。这对我来说很有效。谢谢你为我节省了4个小时:)