#ifndef CLASSA_H
#define CLASSA_H
#include "classB.h"
class ClassA
{
public:
// constructor, other public members
private:
ClassB *child;
// other private members
};
#endif
#ifndef CLASSB_H
#define CLASSB
#include "classA.h"
class ClassB
{
public:
// constructor, other public members
private:
ClassA *parent;
// other private members
};
#endif
How could I do this without the compiler (VC++ 8) giving me
Kevin McGuire wrote:You forgot about the: #ifndef CLASSB_H
#define CLASSB_H
That's a typo. He was stuck with a cyclical reference problem. Solution is to forward declare all you need to know and can forward declare, and to only include the rest. The only case you leave then is the case in which you actually have a recursively-composed structure, which doesn't work by definition since it would be infinitely large.
I figured he might have missed it, and I did not want to to cost him a half or whole hour of looking when he double includes indirectly the same header and the compiler starts reporting that there are two definitions for something.
Kevin McGuire wrote:I figured he might have missed it, and I did not want to to cost him a half or whole hour of looking when he double includes indirectly the same header and the compiler starts reporting that there are two definitions for something.