Reputation: 5301
I am making a project for my college and then there a problem occurred .. i knew it is time to come back here.
There are many files (10-12.. too many for me ) and each one has a header file. i have made separate folders like :
Register Folder : RegistersName.h Register.h (* == corresponfing cpp) files *.cpp
Database Folder : RegisterDB.h InstructionDB.h *.cpp
Instrucion Folder : Instrucion.h *.cpp
Compiler Folder : Compiler.h *.cpp
Command Folder : Command.h *.cpp
Now many of these header files are "seeing" each other therfore i have to include them..
should i include like
In file Compiler.h
#include "../Database/RegisterDB.h"
#include "../Register/Register.h"
#include "../Register/RegistersName.h"
I sincerely think that it is not the write way to do that. because shifting some files here and there would ruin the project .:( please guide me.
thanks for helping me everytime.
Upvotes: 0
Views: 956
Reputation: 94625
The #include statements should be sorted and grouped. Please take a look at C++ Programming Style Guidelines and Stackoverflow thread - Best practices for use of C++ header files.
#include "app/Database/RegisterDB.h"
#include "app/Register/Register.h"
#include "app/Register/RegistersName.h"
Upvotes: 0
Reputation: 92211
You should definitely skip the relative addressing using ..\
because that definitely adds to the coupling.
You can then add the project directory to the include path (however that is done in your build system) and include like
#include "Database/RegisterDB.h"
#include "Register/Register.h"
if you feel like they are "subprojects" that should be kept separately.
Upvotes: 2
Reputation: 206508
Your each include file should have a Include Guard. This prevents the header file from getting included more than once accidentally.
Each source file should include All the header files it needs to. Do no prefer including headers through inclusion in other header files rather include each file which is needed in the source file individually, this avoids the fiasco that if certain header is no longer needed and removed then you get stuck with compilation errors, if that particular header included some other header files which were needed.
Add all paths of where your includes are to the Include path so that you don't have to play around with explicit path mention while including.
If you have circular dependencies you use Forward Declarations of types to get through.
Upvotes: 1
Reputation: 75913
Yes, moving header files around would mean you'd have to fix those includes. But if you have to include, you have to include...
The question is whether or not you really have to include headers in other headers? Sometimes you do, for example if a class inherits from another class. But sometimes people include headers in another header when they really should have only included the header in the source file. So be sure your not creating an unnecessary coupling here. For example, do all users of the Compiler class have to also use stuff from the RegisterDB class? Or is it an implementation detail, and thus should only be included by Compiler.cpp?
Upvotes: 0