I'm getting error code - error: expected primary-expression before 'dice_roll' in my constructor. What am I doing wrong?
Edited on 2/28/17
I've edited this code to my most recent attempt. Now I'm getting 2 errors, both in line 22:
error: random_number_gen was not declared in this scope
error: dice_roll was not declared in this scope
If you move this out of your initialization list you'll see it's the problem. random_number(uniform_int_distribution<int> dice_roll (1, 100)) (line 22).
Generally initialization by function calls should be done inside the constructor's body especially if something's kind of complex.
Unlike the srand and rand functions, your dice_roll is not directly linked to the seed. You need to pass them in as input to dice_roll().
I'm pretty sure you initialized dice_roll correctly in the top-most code, but your problem was that it doesn't automatically return a random number (I don't see a return for uniform_int_distribution<int> so I think it's void on initialization). A random number is only returned when you pass it a random number generator as in the example code (link in my previous post). Your number generator is the mt19937 object, so you have to either make that an internal member, or you have to pass it to your constructor to be used there.
My thought is that you just need to initialize your random_number variable inside the body of the constructor . You can leave the initialization of dice_roll in the initialization list if you want to, but it needs to be initialized before you pass it the generator to get a value for random_number...
1 2 3
initialize your rand num generator
initialize dice_roll
pass the rand num generator to dice_roll() to get value for random_number.
I'll have to study on this some more and try to get random_number initialized in my constructor. Easy with srand but not so easy with mt19937 + uniform_int_distribution<int>.
Got it! I finally realized that all of the variable names in the constructor initialization list must also appear in the private variable list (even those in parentheses). I was also going to try setter/getter functions but realized I didn't need it; I only needed to add a function to get the random number. Not sure if this is exactly correct, but here is the complete working code below: