The problem is that in your globals.h header file, you declare a set of variables that have an external link by default: namely, all global variables!
When you prototype a function in the header file, you declare the function, but do not define it. It is completely legal to have several declarations of the same function, so if several different files all #include the same header and declare the same function, this is completely normal. On the other hand, if you have global variables in the header file, you define these variables. Variables can only be defined once in C ++ (this is called a rule of one definition), and if several files define the same variable or function, this will cause a linker error because the linker will not know which version to use. By the way, by the way, you are not making #include .cpp files, since if you did, you would multiply all the functions exported by this header.
To fix this problem, in the header you will want to change these variable definitions to variable declarations using the extern keyword:
//player stats extern std::string name; extern double str; //strength extern double wis; //wisdom extern double ref; //reflex extern double hp; //health points extern double i; //initiative extern double inte; //intelligence extern double c; //courage extern int gold; //gold extern int xp; //experience extern extern int ap; //armour points extern int wd; //weapon damage extern int lvl; //level extern int sp; //skill points
This will allow you to use any number of files for #include this header, since none of them defines variable names; they simply declare that the variables will exist somewhere. Then you should create a new .cpp file, possibly globals.cpp, which actually defines the variables:
#include "globals.h" std::string name; double str;
These are actual definitions of variables, and since they exist in only one place (globals.cpp), you will not get more linker errors.
Hope this helps!
source share