By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,925 Members | 1,134 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,925 IT Pros & Developers. It's quick & easy.

cyclic includes

P: n/a
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
Jul 23 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Christian Christmann wrote:
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

Jul 23 '05 #2

P: n/a
> 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.
Jonathan

-Chris

Jul 23 '05 #3

P: n/a
Christian Christmann wrote:
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.

Jul 23 '05 #4

P: n/a
Christian Christmann wrote:
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.

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.


Well it looks like you're stuck.
Jonathan

Jul 23 '05 #5

P: n/a
>
Forward declare C in B and A in C.


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

Thank you

Chris

Jul 23 '05 #6

P: n/a
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.

Jul 23 '05 #7

P: n/a
Christian Christmann wrote:

Forward declare C in B and A in C.


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.

Jul 23 '05 #8

P: n/a
> '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

Jul 23 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.