lundi 9 janvier 2017

c++ making random engine static

Im trying to make the random engine for my random helper class:

Its workign when im using this code:

Helper.h

#pragma once
class Helper
{
public:
    Helper();
    ~Helper();
    static int getRandomInt(int min, int max);
    static double getRandomDouble(double min, double max);
};

Helper.cpp

int Helper::getRandomInt(int min, int max)
{
    static std::default_random_engine randomEngine{};
    using Dist = std::uniform_int_distribution<int>;
    static Dist uid{};
    return uid(randomEngine, Dist::param_type{ min,max});
}

double Helper::getRandomDouble(double min, double max)
{
    static std::default_random_engine randomEngine{};
    using Dist = std::uniform_real_distribution<double>;
    static Dist uid{};
    return uid(randomEngine, Dist::param_type{ min,max });
}

So I thought why not use the same random engine and tried:

Helper.h added:

static std::default_random_engine randomEngine;

helper.cpp changed:

std::default_random_engine Helper::randomEngine = std::default_random_engine{};

int Helper::getRandomInt(int min, int max)
{
    //removed randomeEngine 
    using Dist = std::uniform_int_distribution<int>;
    static Dist uid{};
    return uid(randomEngine, Dist::param_type{ min,max});
}

double Helper::getRandomDouble(double min, double max)
{
    //removed randomeEngine 
    using Dist = std::uniform_real_distribution<double>;
    static Dist uid{};
    return uid(randomEngine, Dist::param_type{ min,max });
}

When I try to run it I get 10 errors:

default_random_engine': is not a member of 'std'

missing type specifier - int assumed

When I add an static int (just to test) like:

Helper.h

static int test;

Helper.cpp

int Helper::test = 3;

There are no compile errors, what is the difference between creating a static random engine and a static int?




Aucun commentaire:

Enregistrer un commentaire