So I've been putting a lot of my variables (i.e. strings, ints, chars etc) into my header files for all my classes that I am creating. What I have noticed today is that by doing so I often get stackdumps when I try and access methods that use these variables. However if I remove the variable from the header file and place it directly into my cpp file it works a treat. Are we only supposed to use method declaration inside c++ header files? If not why would this be occurring (all the variables are private but are being accessed via set and get methods).
Thanks
In main.cpp:
GameManager gamemngr;
GameManager.h
#include <string>
#include "Location.h"
#ifndef GAMEMANAGER_H_
#define GAMEMANAGER_H_
class GameManager {
public:
GameManager();
Location* curLoc;
std::string gethelpMenu();
void movePlayer(int i);
private:
Location one, two;
std::string helpMenu;
void initialiseLocations();
};
#endif /* GAMEMANAGER_H_ */
Location.h
#include <string>
#ifndef LOCATION_H_
#define LOCATION_H_
class Location {
public:
Location();
void setEdges(Location *n, Location *e, Location *s, Location *w);
Location* getEdge(int i);
void setDescription(std::string s);
std::string getDescription();
private:
Location* pathways[];
bool blocked[4];
bool locked[4];
};
#endif /* LOCATION_H_ */
If I add a std::string description;
to the location header and then try and access it via curLoc->getDescription
it just stack dumps as soon as it gets to that line in the program. I'm assuming my pointer is pointing to invalid memory but curLoc has the same memory address as the object "one". Am I incorrectly instantiating my objects or something?
EDIT: I will also add I do set it to a default value in the constructor to make sure the string is properly initialised but that has no effect.
Location Implementation (with description placed inside the header file as per my original implementation):
#include "Location.h"
#include <string>
#include <iostream>
Location::Location() {
description = "";
for (int i = 0; i < 4; i++) {
pathways[i] = NULL;
blocked[i] = false;
locked[i] = false;
}
}
void Location::setEdges(Location *n, Location *e, Location *s, Location *w) {
pathways[0] = n;
pathways[1] = e;
pathways[2] = s;
pathways[3] = w;
}
Location* Location::getEdge(int i) {
if(pathways[i] == NULL) {
return this;
} else {
return pathways[i];
}
}
void Location::setDescription(std::string s) {
description = s;
}
std::string Location::getDescription() {
return description;
}
I should probably also add this only seems to be happening with my description string and not the edges methods I have defined as well, as far as I can tell they are working (I need the descriptions to double check my pointers location to be sure but it doesn't stackdump or throw errors).