invalid use of incomplete type (co-dependant classes and forward declaration)

Feb 12, 2010 at 11:40am
Hi All,

I just signed up after i found a partial answer here to a problem i'm facing.

-I have a class (class A) which has a reference to an event handling class (class B).
-class B then can't include class A.h because it would be mutually dependant (A includes B already), so i use forward declaration.
-all is well until i try to use a reference in class B to class A with classA->method().

then i get a compile error invalid use of incomplete type ClassA.

as someone suggested in this post:
http://www.cplusplus.com/forum/general/10624/

i think the problem is:
"A forward declaration allows you to declare a variable of pointer type, but before you actually use it the compiler must see the complete definition. The error message indicates that this is not happening."(user:Hammurabi)

my problem is how to make this happen? Is forward declaration really a solution to the problem of mutually dependant classes?

please help... (happy to post actual code if necessary)

Liam


Feb 12, 2010 at 12:28pm
Does each of the classes contain a member object of the other class?
Feb 12, 2010 at 2:04pm
If I'm understanding correctly, you should review the Inclusion Model vs. Separation Model to get this to work.
Last edited on Feb 12, 2010 at 2:06pm
Feb 12, 2010 at 2:59pm
solved.

The answer is:

classB.h contains a forward declaration

class classA;

then classB.cpp
contains

#include "classA.h"

i.e. the dependancy problem only happens if
#include "classA.h" is in classB.h

once done i can use the pointer no problem.

morecm wrote:
If I'm understanding correctly, you should review the Inclusion Model vs. Separation Model to get this to work

thanks, i'd like to look at that. Do you have a good reference for it?

Feb 12, 2010 at 4:01pm
Topic archived. No new replies allowed.