I havent been programming for a while now and i am trying to make a tex Sim program but im stuck on the first part here, i want to make it so i can use the variable happiness in main but i keep getting an error
void happy(int happiness = 0)
if(hunger > 40)
happiness += 15;
cout << "happiness " << happiness << endl;
int choice = 0;
cout << "Add happiness 1" << endl;
cin >> choice;
if(choice = 1)
happiness += 20;
cout << happiness << endl;
C:\Users\Chay\Desktop\Moods\main.cpp||In function 'int main()':|
C:\Users\Chay\Desktop\Moods\main.cpp|46|warning: suggest parentheses around assignment used as truth value|
C:\Users\Chay\Desktop\Moods\main.cpp|48|error: 'happiness' was not declared in this scope|
||=== Build finished: 1 errors, 1 warnings ===|
The reason you have an error is because happiness is private. The whole idea of classes is to have all the member variables private, then provide public functions which give access to those member variables. I don't mean you should have a public get / set function for each one, because that is the same as making all the variables public.
Instead think about how things might happen in real life. For example if you a class called CPerson which had Names, Address, Phone numbers, Email addresses etc, you could provide functions that set these things in their respective groups, all in one go. That is say a setAddress function which sets the Address1, Address2, Address3, Suburb, PostCode, State etc member variables all in one function.
The happiness variable is a private data member of the player class. That means you can only access that variable with functions that are members of the class or are declared as friend functions. There are reasons for this split that are a little more then I can explain here so it'd be best to grab a beginner tutorial or a reference on classes and read that instead. Classes and OOP are a deep topic that needs to be taken slowly to properly utilize them.
Class member variables are supposed to be private, so that only objects of that class can change their values, and not just be changed by any other function.
For example, if I have a square object, I don't want just anyone to be able to go and change the value of one of it's corner coordinates - now it isn't a square any more. I also don't want anyone to be able to move the whole thing to a different location.
Same Idea for a bank account. You wouldn't want random people changing your account balance, the name of the account, transaction details etc.
So there is the idea of having private variables, with public functions that operate on them. The functions can only be called through the object of that class type. It gives the functions a chance to ensure that the object is always valid (A square is still a square). It also means that only valid operations can happen to an object.
Now, you've got two variables named 'happiness'. One is a private member variable, the other is an argument. When two variables with the same name exist within the same scope, the more localized variable takes precedence.
You should be able to solve your problem now, I think.
I still dont understand, im not sure why, i read all your posts and looked at the link to the documentation on this site but its still hard for me to grasp. i was following a tutorial and this is what i got, please tell me if what he did was right and if there is any way it can be made simpler, i dont want to learn what he did and find out its wrong or theres an easier way of doing it.
0. You have no Constructor/Destructor
1. You are writing function bodies inside the class declaration.
2. You are not initialising your member variables.
3. You are using the same names for multiple variables
4. You are trying to access a member variable of an object by just using its name. (Should be theObject.m_variable).
5. The variable you are trying to access is private so even if you were doing the previous part right, it is still wrong.
The example you posted with the Rectangle. Why did you not follow the ways that was done? I suggest going over the basics of creating variables, creating classes, variable scope, variable access etc.
What the tutorial showed you was absolutely fine. Most folks separate the class declaration into a header file, then the definitions into a separate source file, then you have main in another source, but for small stuff like that it's no biggie.