Modify variables between classes

I'm making a dice rolling program that is to roll a dice and then store the value in an array associated with a player.

First I have a class function rolling the dice, and then I try to assign these variables to a class for a player. This however gives me the error message
"object missing in reference to 'computer::rolls' ". Tried change computer::rolls to computer.rolls, no change.

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
dice.cpp:

 #include <iostream>

#include <cstdlib> 

#include <ctime>
#include "dice.h"
#include "players.h" 
 
using namespace std; 
 
 #define numberOfDices 2

void dice::rollAndShow()
{     //Assign all the players dices a random number
 
      for (int i=0 ; i<numberOfDices; i++)
     {
        computer::rolls[i]= rand() % 6+1;  //Problematic line
     }
     
  
}
 

Players.h: 


class computer

{ 

    public: 

            

    char highestRoll;

    char rolls[numberOfDices];

    char winFlag;

    

    private: 

   

    

};
rolls is not a static member of the class computer. You can only access it through an instantiated object of that class.

If you want to access it through computer::rolls, then declare it as a static variable. You will then need to define it somewhere outside the class.
calls is a non-static data member of class computer. So every calls belongs to a separate object of type computer. To access calls you must specify the object to which it belongs.
Alright, so it's a little bit like local variables in regular functions that only exists as long as the function in in use?

How can this be solved in the neatest way? What about using heritage?
no, it is not at all related to local vs global scope issues.

Whenever you define a class, you have to instantiate an object of that class to access its public members.
1
2
3
4
5
6
7
8
9
10
11
void dice::rollAndShow()
{     //Assign all the players dices a random number

     computer myComputer; // myComputer is now an object of the class computer.
      for (int i=0 ; i<numberOfDices; i++)
     {
        myComputer.rolls[i]= rand() % 6+1;  //now you can access the member rolls.
     }
     
  
}

You may want to brush up your class fundamentals by going through the tutorial on this site.
Aha. I've tried to read the tutorials but I find it hard to understand anyway.

This instance myComputer isn't accessible from other parts of the program right?

I tried a similar program and tried to make an instance that assigned some variables in the instance, and then later in the code tried to access that instance, but that didn't seem to work.

I mean, say I roll some dices in an instance like that and than later want to access the rolls from that particular instance.




So I did my RTFM. Now I kind of get what this is about. This video about classes was great:
http://www.youtube.com/watch?v=QKCfQtSX8Rg

The guy in the video says that it is common practice to access private variables inside classes in a fashion were you create access functions like

1
2
int getMyPrivateVar()            return privateVar;
void setMyPrivateVar(int x)             privateVar=x;


Is this how its almost always done? Feels like a hassle? Better ways?
Last edited on
yes this is how 'private' variables can be accessed from outside functions. Ofcourse you need to have the statements braces.
1
2
 int getMyPrivateVar()        { return privateVar; }
void setMyPrivateVar(int x)    { privateVar=x; }



Other way, declare them as public variables. You can then access them directly from outside, as is done in your computer class.
Last edited on
Topic archived. No new replies allowed.