LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-12-2010, 05:54 AM   #1
quantum_leaf
LQ Newbie
 
Registered: Jul 2005
Posts: 10

Rep: Reputation: 0
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
 
Old 02-12-2010, 06:08 AM   #2
a4z
Senior Member
 
Registered: Feb 2009
Posts: 1,727

Rep: Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742
Quote:
Originally Posted by quantum_leaf View Post
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.
 
Old 02-12-2010, 06:23 AM   #3
quantum_leaf
LQ Newbie
 
Registered: Jul 2005
Posts: 10

Original Poster
Rep: Reputation: 0
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.
 
Old 02-12-2010, 06:39 AM   #4
a4z
Senior Member
 
Registered: Feb 2009
Posts: 1,727

Rep: Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742
Quote:
Originally Posted by quantum_leaf View Post
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
 
Old 02-12-2010, 07:42 AM   #5
quantum_leaf
LQ Newbie
 
Registered: Jul 2005
Posts: 10

Original Poster
Rep: Reputation: 0
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] error: array type has incomplete element type deathsfriend99 Programming 5 02-01-2010 09:36 AM
invalid use of incomplete type ‘struct Display’ rexStart Programming 3 09-17-2008 05:31 AM
error: array type has incomplete element type duduarbel Programming 8 05-27-2008 12:25 PM
arrays of elements with [gcc4]array type has incomplete element type lmmix Linux - Software 0 02-26-2005 08:07 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration