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.
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.
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.