Passing an array from class to function

I want the int game[] array to get values from my function int print();, but get the error "invalid types 'int[int]' for array subscript".

The class:
1
2
3
4
5
6
7
8
9
class Dice
{
    public:
        static const int TIMES = 10;
                     int game[TIMES];

        int roll(int TIMES);
        int print(int game);
};


Main function (not completed):
1
2
3
4
5
int main()
{
    Dice user;
    user.roll(Dice::TIMES);
}


Function roll:
1
2
3
4
5
6
7
8
9
10
int Dice::roll(int TIMES)
{
    srand(time(0)+rand());

    for(int i=0; i<TIMES; i++)
    {
        int dots=rand()%6+1;
        Dice::game[i]=dots;
    }
}


Function print:
1
2
3
4
5
6
7
int Dice::print(int game)
{
    for(int i=0; i<Dice::TIMES; i++)
    {
        cout << game[i] << " ";    //  <- ERROR ON THIS LINE!
    }
}


Please dont judge me to hard. A noob error, I know. :)

Just cant figure it out on my own...
1
2
3
4
5
6
7
int Dice::print(int game) <-- THIS IS THE PROBLEM
{
    for(int i=0; i<Dice::TIMES; i++)
    {
        cout << game[i] << " ";    //  <- ERROR ON THIS LINE!
    }
}


The problem is that the game-parameter is simply an int, not an int-array. Change it to: int Dice::print(int *game) and you should be ok
I changed int game to int *game in the class and in the function print. Now it compiles.

But how do I call the function from main? I tried using the following:
user.print(Dice::game)

And get error
invalid use of non-static data member 'Dice::game'


I get a feeling of being way out of my league...

Dont I have to use pass by reference, pointers and stuff to make this work? Just thinking... will my program work once I'm done?
Do you have an instance of the Dice-class? The correct way to call that function would be something like user.print(dice.game);, assuming dice is an instance of Dice.
Dice is the name of the class, I only have two variables in Dice: the static const int TIMES and the integer array game.
This is what I have so far...

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
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

class Dice
{
    public:
        static const int TIMES = 10;
                     int game[TIMES];

        int roll(int TIMES);
        int print(int *game);
};

int main()
{
    Dice user;
    user.roll(Dice::TIMES);
    user.print(Dice::game);
}

int Dice::roll(int TIMES)
{
    srand(time(0)+rand());

    for(int i=0; i<TIMES; i++)
    {
        int dots=rand()%6+1;
        Dice::game[i]=dots;
    }
}

int Dice::print(int *game)
{
    for(int i=0; i<Dice::TIMES; i++)
    {
        cout << game[i] << " ";
    }
}


And now I get an error on row 21:
invalid use of non-static data member 'Dice::game'
Last edited on
The functions roll() and print() do not need a parameter at all, because TIMES & game are class variables.

In a class function, you do not need to use the scope resolution operator (example Dice::TIMES) because you are referring to a member variable.

The variables TIMES & game should be private.

HTH
Last edited on
I changed the variables to private, no errors.

But now I get errors:

no matching function for call to 'Dice::roll()'
note: candidates are: int Dice::roll(int)

and

no matching function for call to 'Dice::print()'
note: candidates are: int Dice::print(int*)


I only removed (Dice::TIMES) and (Dice::game) from main.
Last edited on
You have to change the declaration of the functions as well.

int Dice::roll()

If the function is to return an int then you do this explicitly in the function with a return statement. If it doesn't need to return a value, then make the return type void:

void Dice::roll()

HTH
And that's the way the cookie crumbles!

Thank you all. Now it finally works!
Topic archived. No new replies allowed.