Accessing "private" data in a class

Hello, I'm back again, still working on the same project ( a simple combat game). I am stuck trying to access the private data in my classes. I have worked my way around it fairly easily, but I have read time and time again, that for stuff like this, it's a good idea to keep these types of things private.

Here is my "monster" header file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
##include "stdafx.h"
#include <iostream>
using namespace std;

class monster
{
public:
	
	int M_attack();
	int M_be_attacked();
	int M_get_strength();
	int M_get_health();
	int M_get_damage();
		
private:

	int M_damage;
	int M_health;
	int M_strength;
	
};


And here is the monster.cpp file (neither of these are complete, I'm just testing stuff out at the moment).

1
2
3
4
5
6
7
8
#include "monster.h"

int M_attack()
{

	cout << "You were struck for " << monster::M_damage<< endl;
}


I am getting an error on this line.
cout << "You were struck for " << monster::M_damage << endl;


It's telling me that member is inaccessible (M_damage). If I change it from private to public it goes away. What would be the best way to pull the data from these variables while still keeping them private?
Last edited on
closed account (zb0S216C)
You can only use the scope resolution operator (::) on a data member if:

A) You're getting the address of a data member
B) You're accessing a static data member or function

Based on your code, you need to instantiate monster. This means you need to create an instance (object) of monster. For example:

 
monster NewMonster;

To access the members of NewMonster, you need to use the member access operator (.), like so:

 
NewMonster.M_attack();

Wazzak
Last edited on

If you're defining a class function, you need to stick the class on the front:

1
2
3
4
5
6
7
#include "monster.h"

int monster::M_attack()
{

	cout << "You were struck for " << M_damage<< endl;
}
@Framework Oh jeeze, I should know better. I had a very similar problem before, and it stemmed from me not creating a member for my class. Here's another question relating to that though. Lets say i create a new member for Fighter, Fighter a; in the Arena () function (fighter is currently the player class i have set up, and arena() is the function where all the combat happens). There is an arena.cpp and a fighter.cpp, so how do i make sure that these members are the same?

Fighter a; in the fighter.ccp is a completely different instance than the fighter a; in the arena.ccp, just like int x; would be, right?

So lets say there is a variable int gold; on fighter.ccp, in my Fighter class. I would want that variable to carry over every where (like the shop function).

closed account (zb0S216C)
Ralden wrote:
"So lets say there is a variable int gold; on fighter.ccp, in my Fighter class. I would want that variable to carry over every where (like the shop function)."
extern. It tells the compiler that the subsequent declaration is defined elsewhere globally in another source file. For example:

1
2
3
4
5
6
7
8
9
10
// SomeFile.cpp
int Something;

/* Functions... */

// SomeOtherFile.cpp

extern int Something; // Grab "Something" from "SomeFile.cpp"

/* Functions... */

Wazzak
Sounds to me that this gold variable is a property of that particular instance of the fighter. Pass the fighter object into the functions that need to know the fighter's internal variables. Making a universal common variable gold wrecks the whole point of having the class.
Excellent, thank you so much for your help. So is this a reasonable way to go about doing this? The thing I love and hate most about coding is that there is more ways than 1 to do something. I've seen many more experienced members here talking about looking back on their early code and seeing lots of unnecessary code. Every time i write something i say to myself "well it works, but there has got to be a better way to do this" haha. Thanks again framework. :)



@moshchops Ah thank you. I was unaware of this, but it makes sense.
you can use get method. like,
1
2
3
4
int getM_damage()
{
    return M_damage;
}


we you need values of M_damage you can just need to call getM_damage() method.
Last edited on
@moschops oh i didn't even see your last post. That is true. I think part of my problem is that i am "thinking too big". I have these great ideas, but executing them or even understanding them is usually above what i know, haha. Thanks again.
@HiteshVaghani1 Thank you. I will try this tomorrow when I get back to my computer. :)
All what you have to do is to specify nested name specifier for the function name

1
2
3
4
5
6
7
#include "monster.h"

int monster::M_attack()
{

	cout << "You were struck for " << monster::M_damage<< endl;
}
Topic archived. No new replies allowed.