My suggestion would be to utilize multiple C++ header and source files; this task is particularly easier if you're using an IDE that allows you to create projects which will make compilation easier.
Let's say you have a bunch of ally and enemy functions in your main file. You can separate these functions definitions neatly into their own
allies.cpp and
enemies.cpp source files respectively. You then create their respective header files,
allies.h and
enemies.h, which holds their function declarations. You can then
#include
the header files in main and enjoy the same access as you had having all the functions in one file. It's the same idea as
#include <iostream>
expect you are making your own headers instead of using system headers. Your project would now look like this:
1 2 3 4 5 6 7 8 9
|
//main.cpp
#include "allies.h"
#include "enemies.h"
int main (void)
{
ally_function();
enemy_function();
}
|
1 2 3 4 5 6 7 8
|
//allies.h - contains allies.cpp function declarations
#ifndef ALLIES
#define ALLIES
void ally_function();
//Declared here. Defined in allies.cpp.
#endif
|
1 2 3 4 5 6 7 8
|
//allies.cpp
#include "allies.h"
#include <string>
void ally_function ()
{
std::cout << "Hi! I'm an ally!" << std::endl;
}
|
1 2 3 4 5 6 7
|
//enemies.h - contains enemies.cpp function declarations
#ifndef ENEMY
#define ENEMY
void enemy_function();
//Declared here. Defined in enemies.cpp.
#endif
|
1 2 3 4 5 6 7 8
|
//enemies.cpp
#include "enemies.h"
#include <string>
void enemy_function()
{
std::cout << "Grr! I'm an enemy!" << std::endl;
}
|
Now a bit of a breakdown.
1. Limit header files to declarations. Use CPP files for definitions. This makes your code more portable and easier to understand.
2. Make sure to comment your declarations liberally. Limits the need to go back and scamper through the source code just to know what a function does.
3. Take note of:
1 2 3 4
|
#ifndef THIS
#define THIS
//declarations here
#endif
|
This is known as a header guard, and it prevents your header files from being unnecessarily duplicated during compilation. The compiler checks to see if "THIS" is not defined; the only time it isn't is when it hasn't been included yet. If it has been defined, then the compiler skips to the #endif life, effectively preventing itself from recopying declarations which were already declared.
4. Notice how I had to
#include "allies.h"
in both main.cpp and allies.cpp. This is because you must include your header file in every individual CPP file you want to access those declarations. Same goes for the
#include "enemies.h"
for the main.cpp and enemies.cpp source files.
5. You've been using this syntax to include files so far:
#include <iostream>
. You must use double quotes when including non-standard files but inequalities when including standard files (cstring, iostream, etc...).
6. Try to make your header/source file pairs as autonomous as possible. Don't make them codependent on each other (i.e. both requiring a data type defined in the other at the same time). This may lead to situations where a type isn't defined by the time it is needed, creating a compilation error.
That's all I got for now. :P