What are some rules with included headers?

I have problems all the time, the bigger my program. For example, I am getting the following error:

In file included from WidgetText.h:8,
                 from LCDText.h:17,
                 from WidgetText.cpp:13:
Generic.h:21: error: expected class-name before ',' token

      

These are the lines:

#include "Generic.h" // WidgetText.h:8

#include "WidgetText.h" // LCDText.h:17

#include "LCDText.h" // WidgetText.cpp:13

class Generic: public virtual LCDText, public CFG, public virtual Evaluator { // Generic.h:21

      

Here are the contents of the various header files:

//Generic.h
#include "CFG.h"
#include "Evaluator.h"
#include "LCDText.h"
#include "Widget.h"

//WidgetText.h
#include "Generic.h"
#include "Property.h"
#include "Widget.h"

//LCDText.h
class Generic;
#include "LCDBase.h"
#include "WidgetText.h"

      

It doesn't do much; I know. I'm not sure what else to include. Each header defines a class named after its header, so LCDText.h has a class named LCDText.

One line declaring the "Generic" class in the LCDText.h file should have been placed there due to an earlier issue like this. I am guessing this current problem has a similar solution, but I haven't found it yet.

+2


source to share


4 answers


Part of the solution is to add some advanced declarations to get rid of these compiler errors (as with your class Generic

line). There will be plenty of suggestions on Google on how to do this.

Using forward declarations will allow you to eliminate the round robin / round robin #include

described in this answer.

The forward declaration allows you to include references and pointers in the forward-declared class, and allows the forward-declared class to be passed as a parameter, but it does not allow you to infer or include an instance-element forward-declared class. So your Generic class needs a way #include

(not just forward-declare) header files for LCDText, CFG, and Evaluator. If it can't do it because LCDText, CFG, or Evaluator requires #include

(and not just forward-declare) Generic, you need to change the hierarchy to fix this (for example, making a member variable a pointer or a class reference instead of make it an instance of the class).



However , using multiple inheritance like this (and especially using diamond inheritance implied by two virtual inheritance) is a definite code smell. This suggests that you should design your class hierarchy in different ways. For example, perhaps you need to maintain composition over inheritance. This will simplify your forward declaration and circular dependencies.

Edit: You mentioned that you have more of a problem with your codebase. I was told that John Lakos Large Scale C ++ Software Development is a good reference for managing issues such as the header of dependent files in large projects, although that might be overkill for where your project is now.

+2


source


You have a circular dependency: Generic.h includes LCDText.h, which includes WidgetText.h, which includes Generic.h; the error arises from this underlying problem. If you can rewrite your headers to eliminate this loop, the chances are that the error will either resolve itself in the refactoring, or the problem will become much more obvious than it is now.



+7


source


From the code presented here, it looks like you are including a header filed multiple times. To prevent problems, you need conditional protections in your header files.

+3


source


Others have already pointed out a circular dependency, but if you are still not sure how to fix it then it searches the whole world like you need to forward the declaration Generic

to WidgetText.h i.e. line 8 becomes

class Generic;

      

If you've tried this already and it sounds like you have, you need to look into how you use Generic

in WidgetText.h and see if you can eliminate the places where you rely on the full definition, eg. change the aggregated Generic

to Generic*

or move the inline element that refers to the method Generic

into an off-line definition in the source file.

+2


source







All Articles