My friend and I are learning C++ and though we have been able to beat down every problem that has come our way (and have learned a bunch by doing so) in this program, this one has been making us rack our brains all day. We have spent countless hours not only trying to solve it ourselves but also looking through Stackoverflow’s related questions and doing tons of Google Searches… Finally we decided that we have to just ask and hope one of you has an idea of what our problem is.
Basically we are trying to create a text based (console only) RPG and we have gotten to the point where we are creating Player Statistics using a class defined in our one and only Header file, there are three constructors to the class (using function overloading of course) and although we are able to run the game error free it seems as though we are not able to actually edit the values of the class member variables in any way!
This is our class:
//File OverHeader.h
class PlayerStatistics
{
public:
PlayerStatistics(int HitPoints, int MagickaPoints, int Fatigue, int Damage, int Defense, int Dodge, int Block, int SpellCastChance);
PlayerStatistics(int Experience, int Level);
PlayerStatistics();
int HitPoints;
int MagickaPoints;
int Fatigue;
int Damage;
int Defense;
// Chance Based System (Relies on Fatigue Level)
int Dodge;
int Block;
int SpellCastChance;
int Experience;
int Level;
};
And the main function:
#include <iostream>
#include <string>
#include "OverHeader.h"
// MAIN FUNCTION DEFINITION
int main()
{
PlayerStatistics PlayerStats(20, 20, 20, 20, 20, 20, 20, 20);
PlayerStatistics PlayerStatsLevel(0, 1);
//continued with code irrelevant to this question.
}
The trouble is that the lines in the Main Function don’t actually set the integer variables in the class to those values. After those two lines the integer values should be set to (just for demonstration purposes):
int HitPoints == 20;
int MagickaPoints == 20;
int Fatigue == 20;
int Damage == 20;
int Defense == 20;
int Dodge == 20;
int Block == 20;
int SpellCostChance == 20;
int Experience == 0;
int Level == 1;
But strangely enough outputting ANY of these integers will simply output random numbers (presumably the memory addresses current values).
The three constructors are in fact defined properly (although not in the Main.cpp) here:
// PlayerCreation.cpp
PlayerStatistics::PlayerStatistics(int HitPoints, int MagickaPoints, int Fatigue, int Damage, int Defense, int Dodge, int Block, int SpellCastChance)
{
}
PlayerStatistics::PlayerStatistics(int Experience, int Level)
{
}
PlayerStatistics::PlayerStatistics()
{
}
And that is our problem, I hope I described everything clearly enough, please let me know if you can help us actually edit the values of these Class Member Variables! All help is appreciated!
This creates the same class in two different ways:
The first creates a variable called
PlayerStatsthat uses the first constructor. The second creates another variable calledPlayerStatsLevelthat uses the second constructor.Now, the constructors…
This doesn’t actually initialise the member variables in the class. You have used the same names in the constructor, but that is actually going to give you grief because (to take a single example) the local variable
HitPointspassed into the constructor overrides the class memberHitPoints. Now, if you want to refer to the class member you must usethis->HitPoints.So, I reiterate, nothing actually got initialised. So you have random values in your object. You need to do this (I’ll take the shorter example):
Note that the other values were not initialized using this constructor. That might be intended, or you might want to set them all to a default value. You need to do this explicitly.
Now, it kinda looks like you wanted a single instance with all those stats set. What you might want to do is use just one constructor – the empty constructor
PlayerStatistics(), and initialize everything to something ‘sensible’. Then define functions to set the stats in chunks:Now in your main:
Or if these are the defaults for a new player, do it in the constructor and then you never need to remember.
Hope that gets you started. Have fun and never be afraid to experiment.