LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   invalid use of incomplete type (co-dependant classes and forward declaration) (https://www.linuxquestions.org/questions/programming-9/invalid-use-of-incomplete-type-co-dependant-classes-and-forward-declaration-788649/)

quantum_leaf 02-12-2010 05:54 AM

invalid use of incomplete type (co-dependant classes and forward declaration)
 
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.

it's seems that before i actually use a forward declared pointer the compiler must see the complete definition. But how to make this happen? Is forward declaration really a solution to the problem of mutually dependant classes?

can anyone help?

(happy to post actual code if necessary)

Liam

a4z 02-12-2010 06:08 AM

Quote:

Originally Posted by quantum_leaf (Post 3861341)
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.

it's seems that before i actually use a forward declared pointer the compiler must see the complete definition. But how to make this happen? Is forward declaration really a solution to the problem of mutually dependant classes?

can anyone help?

(happy to post actual code if necessary)

Liam

if you have classA.h included in classB.cpp (you must do this) than classA.method() could help.

quantum_leaf 02-12-2010 06:23 AM

but that is exactly the problem.

I can't do
#include "ClassA.h" in classB header
because i already have
#include "ClassB.h" in classA header

apparently the solution to this problem was to forward declare classA in classB header, but as this only works as long as i dont use a pointer to classA on it's own it's not much use.

a4z 02-12-2010 06:39 AM

Quote:

Originally Posted by quantum_leaf (Post 3861357)
but that is exactly the problem.

I can't do
#include "ClassA.h" in classB header
because i already have
#include "ClassB.h" in classA header

apparently the solution to this problem was to forward declare classA in classB header, but as this only works as long as i dont use a pointer to classA on it's own it's not much use.

I worte
Quote:

Originally Posted by a4z
if you have classA.h included in classB.cpp (you must do this) than classA.method() could help.



of course you can not include classA.h in classB.h and classB.h in classA.h

in one of your header files you need the forward, and in the implementation file (cpp) you need to include the header you need

that's how forwarding works

quantum_leaf 02-12-2010 07:42 AM

Oh, I see. I have to move the #include of classA to the classB.cpp file and have the forward declaration in the header.
Thanks that works!

I had always assumed that classB.cpp only include classB.h and anything thing the class needs goes in classB.h. But now i see how this doesnt apply for forward declarations like this.


All times are GMT -5. The time now is 07:25 PM.