Reputation: 1096
I've been trying to figure this out by myself for months, and that's not an exaggeration.
I feel dirty using the methods I am not since things tend to break and get out of scope when they get complex and I cannot find an answer to this anywhere.
I have a project structure as follows:
Project-Directory/
main.cpp
makefile
SDLMain.h // SDL libraries required to be in the project directory.
SDLMain.m // SDL libraries required to be in the project directory.
--- gamestate/
------ clean.cpp
------ gamestate.cpp
------ gamestate.h
------ init.cpp
--- graphics/
------ graphics.h
------ // more .cpp files
--- logic/
------- logic.h
------- // more .cpp files
--- character
------- character.h
------- // more .cpp files
In main.cpp
I have:
// C++ Libraries
#include <iostream>
// Graphical Libraries
//#include <SDL/SDL.h>
//#include <SDL/SDL_opengl.h>
// Custom Libraries
#include "character/character.h"
#include "logic/logic.h"
#include "graphics/graphics.h"
#include "gamestate/gamestate.h"
All the .cpp files in character and graphics etc... include their respective header file which shares the same name as the folder. I.e. clean.cpp
, gamestate.cpp
and init.cpp
and all include gamestate.h
.
In each folder there is only one header file, recently reorganised from 1 header file per .cpp.
Basically on this new, more structured system I get scope errors when I attempt to compile my project.
Why is that so if my headerfiles are being included after #include <iostream>
and the SDL libraries in main.cpp
.
I solved the error by inserting this into all header files:
// C++ Libraries
#include <iostream>
// Graphical Libraries
#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>
But then I am including the same thing over and over again and surely this is bad practice.
Not only this, but gamestate.h
contains a prototype used by gamestate.cpp
to a function in logic.cpp
which gets a scope error unless I implicitly include logic.h
in gamestate.h
even though logic.h
is included in main.cpp
before gamestate.h
.
I thought #include
is meant to drag the contents of header files into scope and the functions it prototypes so the compiler knows what to expect.
Why am I getting all these errors about scoping and functions not existing?
Should I make a global.h
and #include
all the SDL and <iostream>
stuff there?
Why can't I access the function prototyped in logic.h
from another file included afterwards in main.cpp
?
Upvotes: 2
Views: 5370
Reputation: 129524
This is sort of a "you can do it many ways, and none are completely right or wrong" question.
But technically, a source file needs to include all headers that it depends on. So if "gamestate.cpp" needs something in "logic.cpp", then "gamestate.cpp" needs to include "logic.h". If EVERYWHERE that uses "gamestate.h" also needs "logic.h", then "gamestate.h" probably should include "logic.h", but I have worked on systems where the rules are: If you are going to use "gamestate.h", you must include "logic.h" first. Note that "gamestate.cpp" is not compiled inside "main.cpp" (unless you commit the heinous crime of including "gamestate.cpp" inside "main.cpp" - but please don't do that).
I like it when you can just use a header file directly, without having to remember the list of header files you must add before it.
Using a "global.h" is probably a bad idea.
Upvotes: 2