I'm trying to build my 2nd text based RPG but this time I wanna make it more flexible and have the code split up and everything. I will post my source code below and my compile error:
If you want to use a global variable in multiple source file. Declare them as extern in a header, and then define them in one source file. I.e.: put extern int i; in a header and in one source file put int i = /* blah blah blah */;
Time to point out the important difference between declaration and definition. You can have as many declarations in your source files as you like, but only ONE definition.
void function();
Is a declaration. It just says "this thingy exists... somewhere".
void function() {...}
Is a definition, because it defines what the function actually is and does.
For variables it's slightly different.
int x;
int x = 10;
Both are definitions, though the first variable isn't initialized (and if I find code like that at work without a really good reason, the author will be verbally slapped).
extern int x;
Is a pure declaration, saying that somewhere this variable exists.
extern int x = 10;
Doesn't make sense, because you now try to declare it AND already define it.
For classes it's a bit more interesting. You generally implement (define) the member functions in a single source file, so everything is fine. Why does it work if you implement them right there in the header (inside the class)? Because it's automatically considered inline. Inline will "fix" your multiple definitions (for functions), but unless you actually want it to be inlined, that kind of thing also deserves a good slapping (especially on very huge and bloated functions, which you shouldn't be coding anyway).
There is also the most important declaration of all. The forward declaration.
class SomeClass;
Again, this simply declares "this class exists somewhere". Get in the habit of using this in your header files instead of including whole header files for other classes. As long as your header is only containing pointers or references to that class, nobody cares what it looks like and by using forward declarations, you reduce messy include avalanches and often reduce compile time. Basically: only use #include in files that actually use the class itself.
Frankly this is one of the reasons why I never have global variables in this form.
It isn’t even simple to explain to him how to fix this how it is.
I use one class per file and as a result this form of a global never exists in my projects.
Instead, they become class static members (read carefully; they are still globals, just in a different form/syntax).
There are several reasons I do this, but the main point here is that it eliminates this headache.
File.h:
class CMyClass {
public :
static vector<int> pRooms;
};
File.cpp: vector<int> CMyClass::pRooms = vector<int>( 10, 0 ); // Will this constructor actually work?
The headache of multiple-definitions and proper use extern is gone.
L. Spiro
I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid