cyclic includes

J

Jonathan Mcdougall

Christian said:
Hi,

I've a problem with including header files.

class A requires header file of class B
class B requires header file of class C
class C requires header file of class A

As can be seen the includes are cyclic. Since
I'm using

#ifndef _FILENAME
#define _FILENAME
...
#endif

in the header files, the include of header file
of class A in class C is not performed.

Is there any way to solve that problem or should I revise
the class design?

Forward declarations usually solve this problem. By declaring, for
example, class B in the header of class A, you don't need to include
the header.

You could perhaps split the headers in different parts. For example, if
the header for class B contains the class definition and something
else, you could put the something else elsewhere. Perhaps then class C
would be able to include that something else from elsewhere.

But if you really need to include them that way, well, C++ can't do
nothing for you.


Jonathan
 
C

Christian Christmann

Hi,

I've a problem with including header files.

class A requires header file of class B
class B requires header file of class C
class C requires header file of class A

As can be seen the includes are cyclic. Since
I'm using

#ifndef _FILENAME
#define _FILENAME
....
#endif

in the header files, the include of header file
of class A in class C is not performed.

Is there any way to solve that problem or should I revise
the class design?

Chris
 
C

Christian Christmann

Forward declarations usually solve this problem. By declaring, for
example, class B in the header of class A, you don't need to include the
header.

I've defined class B in class A.
But then the compiler issues an error message:

field `XXX' has incomplete type

[where XXX is an member attribute of type B defined in class A]
You could perhaps split the headers in different parts. For example, if
the header for class B contains the class definition and something else,
you could put the something else elsewhere. Perhaps then class C would be
able to include that something else from elsewhere.

That's unfortunately not possible since I'm not the author of one of the
huge header files which could possibly be split.
-Chris
 
M

Mercator

Christian said:
Forward declarations usually solve this problem. By declaring, for
example, class B in the header of class A, you don't need to include the
header.

I've defined class B in class A.
But then the compiler issues an error message:

field `XXX' has incomplete type

[where XXX is an member attribute of type B defined in class A]

Forward declare C in B and A in C.
 
J

Jonathan Mcdougall

Christian said:
Forward declarations usually solve this problem. By declaring, for
example, class B in the header of class A, you don't need to include the
header.

I've defined class B in class A.
But then the compiler issues an error message:

field `XXX' has incomplete type

[where XXX is an member attribute of type B defined in class A]

With a forward declaration, you can only refer to the name, not to its
member or its size.

class A;

int main()
{
A *a = 0; // ok
a = new A; // error: size
a->doit(); // error: member
delete a; // error: size
}

Forward declare those you can. For example, if you only have a pointer,
a reference or are only using the name in parameter lists and return
types, you can declare the name. For everything else (inheritance,
objects on the stack, operations requiring size or members), you need
the class definition.
That's unfortunately not possible since I'm not the author of one of the
huge header files which could possibly be split.

Well it looks like you're stuck.


Jonathan
 
C

Catalin Pitis

If you have something like:

class A; // forward declaration

class B
{
A a_;
};

this will generate compiler error, since type B is incomplete (it
depends on type A, which is not defined).

You can solve it by keeping a reference or a pointer to A:

class B
{
A* pa_;
};

You can also rething your design: try to remove cyclic dependencies
between classes, if possible.
 
M

Mercator

Christian said:
And how should the #include <...h> look like?

'Forward declaration' means that you forward declare (but _not_
#include) in header files and #include in source (*.cpp) files. In
practice you can forward declare in headers everything except types for
data members.
 
C

Christian Christmann

'Forward declaration' means that you forward declare (but _not_ #include)
in header files and #include in source (*.cpp) files. In practice you can
forward declare in headers everything except types for data members.

Thank you very much.

-Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top