Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old November 17th, 2004, 07:27 PM
Authorized User
 
Join Date: Nov 2003
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Incomplete Class Declaration

In the book I am learning from, it offers recommendations for getting classes to compile. It says
Quote:
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
__________________
Gill BC
Reply With Quote
  #2 (permalink)  
Old November 18th, 2004, 01:20 PM
Authorized User
 
Join Date: Nov 2004
Posts: 81
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to jkmyoung
Default

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
Code:
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:
Code:
#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 With Quote
  #3 (permalink)  
Old November 19th, 2004, 06:10 PM
Authorized User
 
Join Date: Nov 2003
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
Global declaration of a class object nrlahoti ASP.NET 2.0 Professional 5 October 14th, 2007 01:21 PM
Immediate window, incomplete printout VictorVictor ASP.NET 2.0 Professional 6 March 20th, 2007 08:48 AM
Incomplete example... stavares BOOK: Professional Java Development with the Spring Framework 0 October 6th, 2005 01:47 PM
Incomplete book GeertVerhoeven BOOK: Professional C#, 2nd and 3rd Editions 2 September 25th, 2004 09:39 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.