C ++ header problems

I am creating a small C ++ structure containing a lot of .h and .cpp.

I created a generic include that includes my entire .h file, for example:

framework.h

#include "A.h"
#include "B.h"
#include "C.h"

      

each .h header is protected by include protection like

#ifndef A_HEADER
#define A_HEADER
...
#endif

      

The problem is that I would like to include "framework.h" in all sub.hs for example, but this causes a lot of compiler errors:

#ifndef A_HEADER
#define A_HEADER

#include "framework.h"
...
#endif

      

If instead I use a real header file for each sub-heading, and framework.h for whatever my framework has ever used, it works fine.

I just would like to include the main header in all of my .h routines, so I don't have to include the entire dependency every time.

Thanks:)

+2


a source to share


6 answers


Basically what you do #include "A.h"

in framework.h and #include "framework.h"

in Ah This is causing a circular dependency of the header files and you will get errors like undefined class A. To fix this, use declarations in the header and #include

only in the corresponding cpp file. If this is not possible, I see no other option other than including the individual header files.



+7


a source


Just secure the main header and turn on the guards:

#ifndef FRAMEWORK_H
#   define FRAMEWORK_H
#   include <A.h>
#   include <B.h>
#   include <C.h>
#endif

      



This will prevent recursive inclusion.

+2


a source


You don't have to include the main header file in the sub-header files. It should be used to make life easier for the user, not yours.

Instead, follow these steps:

1) Make cross-definitions of whatever you need in the linked sub-header files.

2) Include in the CPP files only the required subheading files.

3) When you use the framework inside your application code (for example), you can include the main structure header file.

+2


a source


I would recommend using # pragma once and putting that at the beginning of all your header files (framework.h, Ah, Bh and Ch).

Although, if you prefer, I think you could fix your problem simply by adding the include guard in framework.h.

0


a source


I assume you have a dependency between - say B and C, that B depends on C, but in framework.h C is included after B.

0


a source


The circular includes generally a bad idea in C ++. Even though protecting the headers will prevent the preprocessor from going into an infinite loop (or because of an error due to this), you will get unexpected compiler errors because at some point the header will not be included if you think so.

You have to include A.h

, B.h

and C.h

from framework.h

, and A.h

not include framework.h

, just forward declare the classes you use from it. Or do it the other way around: include framework.h

from A.h

, B.h

and C.h

, and forward, declare classes in framework.h

. And, of course, place every code that requires more detailed declarations than, for example, class A

in .cpp files.

0


a source







All Articles