mardi 29 septembre 2015

C++ Header and Source file design implementation

I have a few queries regarding the design principle of laying out a C++ header and source files: I have recently taken over a project in which the previous programmer used to have this, which is particularly annoying because I read somewhere that we shouldn't include a .cpp file in a .hpp file (The preprocessor will just copies and pastes the .cpp file into a .hpp)

Q1. Including a .cpp file in a .hpp file is bad? why?


Due to the problem above, I am facing many "multiple declaration" errors when I load my program in eclipse, even though i added the header guards in all the .hpp files.

Q2. Should i be including the header guards in the .cpp files as well? I tried the later too but to no avail. Any suggestions on this?

Q3. If 2 or more of my .cpp files need the same header files to be used what is the best way to include all those header files? Should i create a new header file say h1.hpp, include all the header files I need in those 2 or more .cpp files and later include in this header file in those .cpp files(s)?

Is it an efficient approach ?

Aucun commentaire:

Enregistrer un commentaire