Accessing member variables through member functions

I thought I understood how to access private/protected class member variables using public functions in the same class. But in this case I'm getting compiler errors. Can anyone help?

Errors:
member 'attackDieSides' is a protected member of class 'Character'
member 'die1' is a protected member of class 'Character'
etc...

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
void dieRoll(Character attacker, Character defender)
{
	srand (time(0));

	// attacker
	if(attacker.attackDieSides == 6)
	{
		attacker.die1 = rand() % 6 + 1;
		attacker.die2 = rand() % 6 + 1;
		attacker.die3 = rand() % 6 + 1;

		if(attacker.attackDie == 3)
		{
			attack = attacker.die1 + attacker.die2 + attacker.die3;
		}
		else if(attacker.attackDie == 2)
		{
			attack = attacker.die1 + attacker.die2;
		}
	}
	else if(attacker.attackDieSides == 10)
	{
		attacker.die1 = rand() % 10 + 1;
		attacker.die2 = rand() % 10 + 1;

		attack = attacker.die1 + attacker.die2;
	}	

	// defender
	defender.die1 = rand() % 6 + 1;
	defender.die2 = rand() % 6 + 1;
	defender.die3 = rand() % 6 + 1;

	if(defender.defenseDie == 3)
	{
		defense = defender.die1 + defender.die2 + defender.die3;
	}
	else if(defender.defenseDie == 2)
	{
		defense = defender.die1 + defender.die2;
	}
	else if(defender.defenseDie == 1)
	{
		defense = defender.die1;
	}
}
Last edited on
I don't see a use of a public method to try to get those members. You are just accessing them normally with the dot operator. Could you explain how you think you are doing that?
This is a public member function of class Character. All the variables it is accessing with the dot operator are also in the Character class, in the private section.
Is this dieRoll function defined within the class definition in a header file? Or is it in a separate cpp file?
dieRoll() is declared in a header file, and defined in an implementation file Character.cpp, which is where this was taken from.
Hah, I see where you're going with that. I forgot 'Character::' before the function definition. :P Thanks for your help guys!
Topic archived. No new replies allowed.