C++ 无法将均匀分布转换为int
我正在尝试制作使用mersenne twister的随机电平发生器。下面是代码,它才刚刚开始,所以没有多大意义: 发电机h:C++ 无法将均匀分布转换为int,c++,c++11,random,visual-studio-2013,mersenne-twister,C++,C++11,Random,Visual Studio 2013,Mersenne Twister,我正在尝试制作使用mersenne twister的随机电平发生器。下面是代码,它才刚刚开始,所以没有多大意义: 发电机h: 我花了很多时间在网上搜索答案,但什么也找不到。请帮助。我认为您的主要问题是将类成员声明为函数。我对您的代码做了一些建议更改,并做了简要说明: class GameMap { public: // are these really meant to be static (one for all GameMap's) // or do you want on
我花了很多时间在网上搜索答案,但什么也找不到。请帮助。我认为您的主要问题是将类成员声明为函数。我对您的代码做了一些建议更改,并做了简要说明:
class GameMap
{
public:
// are these really meant to be static (one for all GameMap's)
// or do you want one set of map data for every GameMap object (non static)?
/*static*/ int bgmap[256][256];
/*static*/ int fg1map[256][256];
/*static*/ int fg2map[256][256];
/*static*/ int genposx, genposy, width, height;
// static std::mt19937 twister(int); // function declaration?
std::mt19937 twister;
// probably you don't want static function declarations
std::uniform_int_distribution<int> dist1; //(int, int);
std::uniform_int_distribution<int> dist2; //(int, int);
static std::random_device rd;
void Generate(int sizex, int sizey, int seed);
GameMap();
~GameMap();
};
GameMap::GameMap()
: dist1(1, 8)
, dist2(1, 248) // initialize these here
{
}
void GameMap::Generate(int sizex, int sizey, int seed = rd())
{
twister.seed(seed); // probably you meant this?
genposx = 1;
genposy = 1;
do
{
genposx = dist2(twister);
genposy = dist2(twister);
width = dist1(twister);
height = dist1(twister);
} while (whatever);
}
那你为什么要把一个统一的int分布转换成int分布呢?这毫无意义。我不相信你在网上搜索答案已经浪费了很多时间,因为有大量的阅读材料在使用统一的int分布的正确方法上,而这些都没有在你的代码中表示出来。您甚至没有为函数dist1和dist2提供正确数量的参数……为什么类中的所有内容都是静态数据成员?您没有为这些静态成员中的任何一个提供定义,并且由于某些奇怪的原因,您在声明它们时一直在编写它们的构造函数参数类型,因此编译器认为它们是成员函数声明。这可能不是你想听到的答案,但你犯的错误表明你会从阅读一篇文章中受益匪浅。@Praetorian-不,这不是我不想听的答案。编程实际上并不是我最感兴趣的事情,我也没有把大部分空闲时间花在编程上。通常我在编程时不使用对象,因为它是针对硬件的低级编程,所以我是这方面的新手。我要买本书,因为我现在学到的关于物体的东西似乎都是胡说八道。无论如何,谢谢你的回复。
//#include <SFML/Graphics.hpp>
#include <random>
#include "Generator.h"
GameMap::GameMap()
{
dist1(1, 8);
dist2(1, 248);
}
void GameMap::Generate(int sizex, int sizey, int seed = rd())
{
twister(seed);
genposx = 1;
genposy = 1;
do
{
genposx = dist2(twister);
genposy = dist2(twister);
width = dist1(twister);
height = dist1(twister);
} while (whatever);
}
no suitable conversion from unifgorm_int_distribution<int>" to "int" exists
argument of type "std::mt19937 (*)(int)" is incompatible with parameter of type "int"
too few arguments in function call
genposx = dist2(twister);
genposy = dist2(twister);
width = dist1(twister);
height = dist1(twister);
class GameMap
{
public:
// are these really meant to be static (one for all GameMap's)
// or do you want one set of map data for every GameMap object (non static)?
/*static*/ int bgmap[256][256];
/*static*/ int fg1map[256][256];
/*static*/ int fg2map[256][256];
/*static*/ int genposx, genposy, width, height;
// static std::mt19937 twister(int); // function declaration?
std::mt19937 twister;
// probably you don't want static function declarations
std::uniform_int_distribution<int> dist1; //(int, int);
std::uniform_int_distribution<int> dist2; //(int, int);
static std::random_device rd;
void Generate(int sizex, int sizey, int seed);
GameMap();
~GameMap();
};
GameMap::GameMap()
: dist1(1, 8)
, dist2(1, 248) // initialize these here
{
}
void GameMap::Generate(int sizex, int sizey, int seed = rd())
{
twister.seed(seed); // probably you meant this?
genposx = 1;
genposy = 1;
do
{
genposx = dist2(twister);
genposy = dist2(twister);
width = dist1(twister);
height = dist1(twister);
} while (whatever);
}