Reputation: 6137
I'm trying to create an object that is supposed to implement some methods for my roulette program, but when compiling I got error that no member generate exist in std::random_device
here is a class example:
#include <random>
class Engine
{
public:
Engine();
int spin();
private:
std::mt19937 m_generator;
std::uniform_int_distribution<int> m_distribution;
std::random_device m_seed;
};
Engine::Engine() :
m_generator(m_seed),
m_distribution(0, 36)
{
}
int Engine::spin()
{
return m_distribution(m_generator);
}
// now let's try it out
Engine eng;
for(int i = 0; i < 20; ++i)
eng.spin();
what am I doing wrong?
the above code opens a std header with code that is not easy to understand
Upvotes: 13
Views: 9361
Reputation: 227418
The engine std::mt19937
requires a seed in its constructor. You are passing it an std::random_device
. It looks like you intended to do something like
Engine::Engine() : m_generator(m_seed()), m_distribution(0, 36) {}
^^
You should also make sure m_seed
has been initialized before it is used. This will require declaring it before m_generator
:
private:
std::random_device m_seed;
std::mt19937 m_generator;
std::uniform_int_distribution<int> m_distribution;
although I suspect you don't really need that member, and you can say
Engine::Engine() : m_generator(std::random_device()()),
m_distribution(0, 36) {}
See this live example.
Upvotes: 22