问题描述
我试图通过在构造函数中初始化它并在私有成员函数中使用它来使用 C++ 随机库。我能够使来自 Generating random integer from a range 的评分最高的答案中的简单代码在没有函数的 main 中工作,但我无法让它在带有标头的基本类中编译。需要什么才能使随机库在这种结构中工作?
兰迪.h
#ifndef Randy_H_
#define Randy_H_
#include <random>
class Randy
{
public:
Randy();
unsigned short int Doit();
private:
std::random_device rd;
std::mt19937 rng();
std::uniform_int_distribution<unsigned short int> InRange1();
std::uniform_int_distribution<unsigned short int> InRange2();
unsigned short int GenerateBar();
};
#endif
Randy.cpp
#include <random>
#include "Randy.h"
using namespace std;
Randy::Randy()
{
std::random_device rd;
std::mt19937 rng(rd());
std::uniform_int_distribution<unsigned short int> InRange1(0,180);
std::uniform_int_distribution<unsigned short int> InRange2(150,15000);
}
unsigned short int Randy::Doit()
{
unsigned short int x = GenerateBar();
return x;
}
unsigned short int Randy::GenerateBar()
{
unsigned short int oof = InRange1(rng);
unsigned short int rab = InRange2(rng);
unsigned short int bar = oof + rab;
return bar;
}
Windows 10 上的 g++ 抱怨如下:
Randy.cpp: In member function 'short unsigned int Randy::GenerateBar()':
Randy.cpp:22:42: error: no matching function for call to 'Randy::InRange1(<unresolved overloaded function type>)'
unsigned short int oof = InRange1(rng);
^
In file included from Randy.cpp:2:
Randy.h:14:59: note: candidate: 'std::uniform_int_distribution<short unsigned int> Randy::InRange1()'
std::uniform_int_distribution<unsigned short int> InRange1();
^~~~~~~~
Randy.h:14:59: note: candidate expects 0 arguments,1 provided
Randy.cpp:23:42: error: no matching function for call to 'Randy::InRange2(<unresolved overloaded function type>)'
unsigned short int rab = InRange2(rng);
^
In file included from Randy.cpp:2:
Randy.h:15:59: note: candidate: 'std::uniform_int_distribution<short unsigned int> Randy::InRange2()'
std::uniform_int_distribution<unsigned short int> InRange2();
^~~~~~~~
Randy.h:15:59: note: candidate expects 0 arguments,1 provided
提前致谢...
在查看了建议的提示 Why should I prefer to use member initialization lists? 后,我已经找到了解决问题的结构。我在这里提供它是因为它可以为其他人节省许多小时,因为它可以解决如何在 C++ 的隐形领域中导航的问题。
Randy2.h
#ifndef RANDY_H
#define RANDY_H
#include <random>
class Randy
{
public:
Randy();
Randy(int seed);
unsigned short int Doit();
private:
int mySeed { 17 };
std::mt19937 genRand;
std::uniform_int_distribution<unsigned short int> InRange1;
std::uniform_int_distribution<unsigned short int> InRange2;
unsigned short int GenerateBar();
};
#endif
Randy2.cpp
#include <random>
#include "Randy2.h"
using namespace std;
Randy::Randy()
:
genRand(mySeed),InRange1(0,180),InRange2(150,15000)
{
// No ctor body needed
}
Randy::Randy(int seed)
:
genRand(seed),15000)
{
// No ctor body needed
}
unsigned short int Randy::Doit()
{
unsigned short int x = GenerateBar();
return x;
}
unsigned short int Randy::GenerateBar()
{
unsigned short int oof = InRange1(genRand);
unsigned short int rab = InRange2(genRand);
unsigned short int bar = oof - rab;
return bar;
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)