Re: [Dev-C++] declaring two classes with pointers to each other?
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Szczepan H. <ru...@po...> - 2001-07-01 14:51:45
|
----- Original Message -----
From: "Phil Schulz" <fy...@qw...>
To: <dev...@li...>
Sent: Sunday, July 01, 2001 1:21 AM
Subject: [Dev-C++] declaring two classes with pointers to each other?
>
> I want to create two classes that contain
> pointers to each other, but I keep getting
> "Syntax error before *" I know a class can
> contain a pointer to itself, but why not to
> others before it is fully defined?
Because the compiler must at least know that the identifier
used before * is a class name. When you define a class with
a pointer to itself, the compiler knows it because the current
class name has already been parsed as such.
class Foo { // Now we know Foo is a class name
public:
Foo* other; // OK, we already know Foo is a class name
};
But here you have a different scenario. Let's say you are
compiling book.cpp. There must be a line in your file that says:
#include "book.h"
Your compiler goes to the file book.h and eventually encounters
this line:
#include "author.h"
Please note that the definition of class Book hasn't been
reached yet.
Now we are in "author.h" and again we include book.h. But now
the content of book.h is skipped due to the BOOK_H define. This
is very important: the definition of class Book gets skipped
and the compiler still doesn't know what Book means!
Now we are back in "author.h" and we are parsing the definition
of class Author.
class Author
{
public:
Book* works[20]; //Ouch! What do you mean by Book?
};
> The classes are declared in their own header files,
> which are reciprocally included in the other... The
> function coding were in separate .cpp files.. What
> am I forgetting?
You are forgetting forward declarations. Here are your
files fixed. I removed the irrelevant "using namespace std;"
directives.
-----author.h----
#ifndef AUTHOR_H
#define AUTHOR_H
#include "book.h"
class Book; //forward declaration
class Author
{
public:
Book* works[20];
};
#endif
---book.h----
#ifndef BOOK_H
#define BOOK_H
#include "author.h"
class Author; //forward declaration
class Book
{
public:
Author* authorP;
};
#endif
Depending on the context, the compiler may actually encounter
some forward declarations _after_ the coresponding class
definition, but don't worry, it's _perfectly_legal_.
If you still don't like the idea of forward declaractions
ending up backward, then you may choose another solution,
one that is used by most code-generating CASE tools. You
can create a separate file in which you put forward
declarations of all classes in your project. You then
include this file from every header in your project, either
before any other include or before any of _your_ includes.
Here is what it would look like:
---library.h---
#ifndef LIBRARY_H
#define LIBRARY_H
class Author;
class Book;
#endif
-----author.h----
#ifndef AUTHOR_H
#define AUTHOR_H
#include "library.h" //forward declarations
#include "book.h"
class Author
{
public:
Book* works[20];
};
#endif
---book.h----
#ifndef BOOK_H
#define BOOK_H
#include "library.h" //forward declarations
#include "author.h"
class Book
{
public:
Author* authorP;
};
#endif
Hope this helps.
Szczepan Holyszewski
ru...@po...
|