我尝试将main中的代码封装到一个类中。main中的代码运行良好。当我将其移动到一个类中时,开始出现编译错误。错误为:错误:'rd'不是型别
#include <random>
#include <iostream>
class StdRandom
{
std::random_device rd; //Will be used to obtain a seed for the random number engine
std::mt19937 gen(rd()); //error
std::uniform_int_distribution<> distrib;
public:
StdRandom(int V)
{
std::uniform_int_distribution<> distribTmp(0, V);
distrib = distribTmp;
}
int uniformInt()
{
return distrib(gen);
}
};
int main()
{
std::random_device rd; //Will be used to obtain a seed for the random number engine
std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd()
std::uniform_int_distribution<> distrib(1, 6);
for (int n=0; n<10; ++n)
//Use `distrib` to transform the random unsigned int generated by gen into an int in [1, 6]
std::cout << distrib(gen) << ' ';
std::cout << '\n';
}
1条答案
按热度按时间z9gpfhce1#