"jimmy" <ji**********@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com
Hi,
I have circular dependancies between a pair of classes; Chicken and
Egg. I changed it to this;
_Chicken.h_
Class Egg;
_Egg.h_
#include "Chicken.h"
This worked fine until I tried to move some functions from Chicken.cpp
to Chicken.h for inlining. The problem is that Chicken.h doesn't have
the Egg's complete interface, only the Class declaration.
Is there a solution for this? Is it possible to forward declare the
functions I need?
The solution is to do all of the declaring first before you start defining,
e.g.,
Chicken_and_Egg.h
class Egg;
class Chicken
{
public:
void foo(Egg* ptr);
};
class Egg
{
public:
void foo(Chicken* ptr);
};
inline void Chicken::foo(Egg *ptr)
{
// stuff
}
inline void Egg::foo(Chicken *ptr)
{
// stuff
}
You could split this into multiple files if you wanted, but I think you will
need to have more than two header files, e.g.,
-------------------------------
Chicken0.h
class Egg;
class Chicken
{
public:
void foo(Egg* ptr);
};
----------------------------------
Egg0.h
class Chicken;
class Egg
{
public:
void foo(Chicken* ptr);
};
-------------------------------------
Chicken.h
#include "Chicken0.h"
#include "Egg0.h"
inline void Chicken::foo(Egg *ptr)
{
// stuff
}
---------------------------------------
Egg.h
#include "Chicken0.h"
#include "Egg0.h"
inline void Egg::foo(Chicken *ptr)
{
// stuff
}
--
John Carson