Cross reference and circular dependency. Indirect title

placeable.h

#include "selectable.h" class placeable : selectable { .. }; 

selectable.h

 #include "game.h" class selectable { .. }; 

game.h

 #include "placeable.h" class game { ... class placeable* holding; ... }; 

Basically placeable.h includes selectable.h, which includes game.h, which includes placeable.h again.

The only solution I can think of is to place the placed * in a new header, which makes it static / global, and then include this new header in game.h and selectable.h.

I'm sorry that I did not include the header blocks in the top code. I assumed this was obvious. The header protector does not help in this case due to inheritance, the same thing happens with the forward declaration.

+4
source share
5 answers

Include only headings if you MUST

Use forward declaration to include:

You need to include a header for class X iff:

  • You have a member of class 'X'
  • You get from class "X"
  • You pass the class parameter "X" by value.

Otherwise, a forward declaration will be available.

 // -> Don't do this #include "placeable.h" class placeable; // forward declare // Fine if you are using a pointer. class game { ... class placeable* holding; ... }; 

PS. Add heading advocates.

+5
source

This means that you did not properly encapsulate the functionality of your design. It should be a higher level, including a lower level, and not one level - one level. If the game is a higher level, then the chosen one should not include game.h.

+3
source

This is a resolved issue. It was called heading guards. Try this inside ALL of your header files:

 #ifndef __NAMEOFTHEFILE_H__ #define __NAMEOFTHEFILE_H__ // nothing goes above the ifndef above // everything in the file goes here // nothing comes after the endif below #endif 

Alternatively, you can do this (this is called a direct link):

 // game.h class placeable; class game { ... placeable* p; }; 
+2
source

There are two problems:

  • Circle heading dependency. The solution is #ifndef ...
  • Allocation of space for an unknown type. The solution is a class that can be placed,

More here

0
source

Use header protection in each header file to avoid this problem. Typically, your header files should look like this:

 #ifndef PLACEABLE_H #define PLACEABLE_H // // Class definitions and function declarations // #endif 
-1
source

Source: https://habr.com/ru/post/1387793/


All Articles