Class - dice_roll error

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <string>
#include <random>
#include <ctime>

using namespace std;

class GuessGame
{
public:
    GuessGame ();
    void introduce ();
    bool check_guess (int& g);
    void run ();

private:
    int random_number;
    int guess;
    int tries;
};

GuessGame::GuessGame() : random_number(dice_roll(random_number_gen)), guess(0), tries(0)
{}

void GuessGame::introduce ()
{
    cout << "\tWelcome to the Number Guessing Game." << endl << endl;
}

bool GuessGame::check_guess (int& g)
{
    if (g < random_number)
    {
        cout << endl << "Guess higher..." << endl;
        return false;
    }

    else if (g > random_number)
    {
        cout << endl << "Guess lower..." << endl;
        return false;
    }

    else
    {
        cout << endl << "Congratulations. You guessed my number." << endl;
        cout << "It took you " << tries << " tries." << endl << endl;
        return true;
    }
}

void GuessGame::run ()
{
    while (guess != random_number)
    {
        cout << "Enter your number: ";
        cin >> guess;
        ++ tries;

        if (check_guess(guess))
            break;
    }
}

int main ()
{
    mt19937 random_number_gen (time(0));
    uniform_int_distribution<int> dice_roll (1, 100);

    GuessGame guess_game;
    guess_game.introduce ();
    guess_game.run ();

    return 0;
}
Last edited on
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.

I don't know much about the random library myself, but this has the example at least...
http://www.cplusplus.com/reference/random/uniform_int_distribution/
(looks like random_number needs to be initialized by dice_roll with an input from a rand num generator)

(You might also want to fix line 47)
Last edited on
Thanks for your reply.

I've made some changes, along with line 47. =)

In line 22 I now have:
GuessGame::GuessGame() : random_number(dice_roll(random_number)), guess(0), tries(0)

And in lines 67 & 68 I have:
mt19937 random_number (time(0));
uniform_int_distribution<int> dice_roll (1, 100);

This is more how it's supposed to be (I think), but now I'm getting error: dice_roll was not declared in this scope.

I tried declaring dice_roll in private or public but getting: expression cannot be used as a function.

Where or how am I supposed to declare dice_roll?

I can get this to work by using srand -

line 22 - GuessGame::GuessGame() : random_number(rand() % 100 + 1), guess(0), tries(0)
line 67 - srand (time(0));

But I want to use the better mt19937. Why can I get srand to work but not mt19937?
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.


If you're going to use the initialization list for all this then please read these two links, they might help if you have problems arising from order of initialization.
http://stackoverflow.com/questions/1242830/constructor-initialization-list-evaluation-order
http://stackoverflow.com/questions/4162021/is-it-ok-to-call-a-function-in-constructor-initializer-list
Last edited on
Thanks again, newbieg.

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <string>
#include <random>
#include <ctime>

using namespace std;

class GuessGame
{
public:
    GuessGame ();
    void introduce ();
    bool check_guess (int& g);
    void run ();

    void get_ran_num (int a)
    {
        random_number = a;
    }


private:
    int random_number;
    int guess;
    int tries;
    int a;
};

GuessGame::GuessGame() : random_number(random_number), guess(0), tries(0)
{}

void GuessGame::introduce ()
{
    cout << "\tWelcome to the Number Guessing Game." << endl << endl;
}

bool GuessGame::check_guess (int& g)
{
    if (g < random_number)
    {
        cout << endl << "Guess higher..." << endl;
        return false;
    }

    else if (g > random_number)
    {
        cout << endl << "Guess lower..." << endl;
        return false;
    }

    else
    {
        cout << endl << "Congratulations. You guessed my number." << endl;
        cout << "It took you " << tries << " tries." << endl << endl;
        return true;
    }
}

void GuessGame::run ()
{
    while (guess != random_number)
    {
        cout << "Enter your number: ";
        cin >> guess;
        ++ tries;

        if (check_guess(guess))
            break;
    }
}

int main ()
{
    mt19937 random_number_gen (time(0));
    uniform_int_distribution<int> dice_roll (1, 100);

    GuessGame guess_game;
    guess_game.get_ran_num(dice_roll(random_number_gen));
    guess_game.introduce ();
    guess_game.run ();

    return 0;
}
Topic archived. No new replies allowed.