I am currently implementing a Deck class that represents a 52 card playing deck. It uses the boost Random library to shuffle integers that represent cards.
#include <iostream>
#include <fstream>
#include "constants.hpp"
#include <boost/program_options.hpp>
#include <vector>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_int.hpp>
#include <boost/random/variate_generator.hpp>
boost::mt19937 gen(std::time(0));
class Deck{
private:
std::vector<int> cards;
int cardpointer;
static ptrdiff_t choosecard(ptrdiff_t i);
ptrdiff_t (*pchoosecard)(ptrdiff_t);
public:
Deck();
void shuffle();
int pop();
};
Deck::Deck(){
for(int i=1; i<=52; i++){
cards.push_back(i);
}
cardpointer = -1;
pchoosecard = &choosecard;
}
ptrdiff_t Deck::choosecard(ptrdiff_t i){
boost::uniform_int<> dist(0,i);
boost::variate_generator< boost::mt19937&, boost::uniform_int<> > cardchoice(gen, dist);
return cardchoice();
}
void Deck::shuffle(){
std::random_shuffle(cards.begin(), cards.end(), pchoosecard);
}
I want to move the “boost::mt19937 gen(std::time(0));” line to be part of the class, however I’m having problems doing so, since I get this error when I move it into the class definition:
$ make
g++ -I /usr/local/boost_1_45_0/ -c main.cpp
main.cpp:22: error: ‘std::time’ is not a type
main.cpp:22: error: expected ‘)’ before numeric constant
main.cpp:22: error: expected ‘)’ before numeric constant
main.cpp:22: error: expected ‘;’ before numeric constant
main.cpp: In static member function ‘static ptrdiff_t Deck::choosecard(ptrdiff_t)’:
main.cpp:39: error: ‘gen’ was not declared in this scope
make: *** [main.o] Error 1
If you’re making it a normal class variable, initialize it in the constructor:
If you’re making it static (it looks like you are, since you’re using
genfromchoosecard, which is static), you still need a declaration outside the class: