Subject: Incomplete Class Declaration
Posted By: gillianbc Post Date: 11/17/2004 6:27:25 PM
In the book I am learning from, it offers recommendations for getting classes to compile.  It says
quote:

1. Always put an incomplete class declaration in your .h files for each class that's referenced within the class definition in the .h file.
2. Always put your #include directives in the .cpp files in a sequence which ensures that each class definition that's included is preceded by the .h files for any dependent classes; these will be the classes for which you added incomplete class declarations.


I'm having trouble getting my head round this and very confused about where all the #includes go.  

  • What do the incomplete class declarations do during the compilation?  

  • Should I put them in the .cpp as well as the .h ?






Gill BC
Reply By: jkmyoung Reply Date: 11/18/2004 12:20:18 PM
The incomplete class declarations in the header usually declare the functions, but have no actual code in them. that way, the amount of code you can include less code when you need to reference the class. The compiler will link it for you later.

eg. Queue.h
class Queue{
public Queue();
public void Enqueue(Object obj);
public Object Dequeue();
public Object Peek();
private Vector myVector;
}
and then in Queue.cpp you'd have the actual code for the functions:
#include "Queue.h"
public void Queue::Enqueue(Object obj){
  myVector.Add(obj);
}
... other functions

I think generally you include files in the header only if you reference them in the header, eg. in this example you'd have to include Vector.h in the header

In the .cpp file, include the header file of the class, as well as the header of any other classes you used that your header doesn't know about.

I hope this makes sense.

Reply By: gillianbc Reply Date: 11/19/2004 5:10:14 PM
Thanks - the exercise I was working with had a dozen or so classes that needed visibilty of each other.  I was trying to understand when to use a #include statement and when to use an incomplete class definition. e.g. in the .h file for class A, the return type of one of the functions was of type class B.  I have to make class A aware of class B. An incomplete class definition was used rather than a #include statement.  But why shouldn't you use an include statement?   In the .cpp file for class A, the #include statement was used ?

Gill BC

Go to topic 22080

Return to index page 711
Return to index page 710
Return to index page 709
Return to index page 708
Return to index page 707
Return to index page 706
Return to index page 705
Return to index page 704
Return to index page 703
Return to index page 702