Reputation: 12331
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:
static int getRandomInt(int min, int max);
static double getRandomDouble(double min, double max);
};
Helper.cpp
#include <random>
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:
//added randomEngine
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?
Note: The first example is working and I have included (else the first example would not work)
Upvotes: 1
Views: 1497
Reputation: 93384
The errors suggest that you're forgetting to include the <random>
header in Helper.h
.
Changing Helper.h
to:
#pragma once
#include <random>
class Helper
{
static std::default_random_engine randomEngine;
public:
Helper();
~Helper();
static int getRandomInt(int min, int max);
static double getRandomDouble(double min, double max);
};
...should fix your issue.
Upvotes: 4