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.
##include "stdafx.h"
#include <iostream>
usingnamespace 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?
@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).
"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
externint Something; // Grab "Something" from "SomeFile.cpp"
/* Functions... */
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.
@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.