无法将 uniform_int_distribution<int> 转换为 int
Can't convert uniform_int_distribution<int> to int
我正在尝试制作使用 mersenne twister 的随机关卡生成器。代码如下(刚刚开始,意义不大):
Generator.h:
//Level generator
#pragma once
class GameMap
{
public:
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);
static std::uniform_int_distribution<int> dist1(int, int);
static std::uniform_int_distribution<int> dist2(int, int);
static std::random_device rd;
void Generate(int sizex, int sizey, int seed);
GameMap();
~GameMap();
};
Generator.cpp:
//#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);
}
问题是我无法将 uniform_int_distrubution 转换为 int。我收到 Intellisense 错误消息:
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 成员声明为函数。我对您的代码进行了一些建议的更改,并附有简要说明:
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);
}
我正在尝试制作使用 mersenne twister 的随机关卡生成器。代码如下(刚刚开始,意义不大):
Generator.h:
//Level generator
#pragma once
class GameMap
{
public:
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);
static std::uniform_int_distribution<int> dist1(int, int);
static std::uniform_int_distribution<int> dist2(int, int);
static std::random_device rd;
void Generate(int sizex, int sizey, int seed);
GameMap();
~GameMap();
};
Generator.cpp:
//#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);
}
问题是我无法将 uniform_int_distrubution 转换为 int。我收到 Intellisense 错误消息:
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 成员声明为函数。我对您的代码进行了一些建议的更改,并附有简要说明:
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);
}