无法将uniform_int_distribution <int>转换为int

时间:2015-07-20 17:14:42

标签: c++ c++11 random visual-studio-2013 mersenne-twister

我正在尝试制作使用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);

我在网上搜索答案时已经失去了很多时间,但我找不到任何东西。请帮忙。

1 个答案:

答案 0 :(得分:4)

我认为您的主要问题是将您的班级成员声明为函数。我对您的代码做了一些建议的更改,并提供了简短的解释:

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);
}