-1

Let's say I want to compile something like this:

//Prova.h:
//--------------------
#ifndef _PROVA_
#define _PROVA_

#include "Terza.h"

class Prova{
public:
 Prova();

};
#endif

and

//Terza.h:
//--------------------
#ifndef _TERZA_
#define _TERZA_

#include "EreProva.h"

class Terza{
public:
  Terza();
};
#endif

and

//EreProva.h:
//--------------------
#ifndef _EREPROVA_
#define _EREPROVA_

#include "Prova.h"

class EreProva : public Prova{
  public:
  EreProva();
};
#endif

which doesn't compile saying "'Prova' : base class undefined".

What is the best way to avoid recursion of header between inherited classes?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Luke Givens
  • 829
  • 1
  • 11
  • 15

3 Answers3

4

If you need to have cyclic dependencies there is something wrong with your design and you should revisit your design and try to remove such complex and unwanted cyclic dependencies.

One of overcoming cyclic dependencies is to use Forward Declarations, but note that once you forward declare a type the type becomes Incomplete type for the compiler and there are limitations about what operations you can do with it. You cannot perform any operations on that type instances which need the compiler to know the memory layout of the type.

Good Read:
When can I use a forward declaration?

Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • +1 for mentioning the broken design. **That is the important bit**, not some syntactic trick to get around it. – John Dibling May 31 '12 at 14:47
  • Let's say Terza is a map. Prova is something that want to move on that map. EreProva is a basic square of that map. To move Prova i need to know the map. To draw the map i need to use the squares. – Luke Givens May 31 '12 at 14:52
  • oh i also forgot to mention that forward declaration didn't work unless i'm doing something wrong – Luke Givens May 31 '12 at 15:51
0

Sometimes you can work around problems of this sort by tring the following: (1) try adding the "#pragma once" directive at the top of your files, although this may be compiler specific (I used it when developing in VC++ some time ago) (2) instead of including the header files in the class, you can try just add "class Prova", or whatever class it is, to indicate a class which you will define later on but want to "use" now.

Although as Als says, it is better to avoid such designs.

0

In this code:

//Prova.h:
//--------------------
#ifndef _PROVA_
#define _PROVA_

#include "Terza.h"

class Prova{
public:
 Prova();

};

Since you don't use the Tezra class in any way, you don't need the #include. Take it out. Also, you are missing and #endif. Close the #ifndef in this file with a matching #endif in this file.

Moreover:

//Terza.h:
//--------------------
#ifndef _TERZA_
#define _TERZA_

#include "EreProva.h"

class Terza{
public:
  Terza();
};
#endif

#endif

You also don't use the EreProva class in this file at all -- so take out the #include statement. You also have an extra #endif at the end of the file. There is only one #ifndef here, so there should only be one #endif. Take the last one out.

John Dibling
  • 99,718
  • 31
  • 186
  • 324
  • i know i'm not using the includes, this was just an example – Luke Givens May 31 '12 at 15:16
  • Please post actual code that demonstrates the real problem you're having. Then we can give you better advice. – John Dibling May 31 '12 at 15:17
  • Thanks for you replies. My problem is how to compile the thing.. I need to use all the headers included. For example instantiating a variable EreProva in Terza and a variable Terza in Prova. I'd wish to know if there's some method other than rethinking the classes dependencies – Luke Givens May 31 '12 at 15:37