Two classes which contain pointers that point at the other class (C++)

Started by
1 comment, last by jtedit 12 years, 8 months ago
Hi, I would like to know how to make it so class A can have a pointer to an instance of class B while class B can have a pointer to am instance of Class A.

Ive tryed defining them in two files:

ClassA.h:

#include "ClassB.h"

class cA{
private:
cB* pointer;
};


ClassB.h:

#include "ClassA.h"

class cB{
private:
cA* pointer;
};


but get errors saying class B is undefined and it cant make a pointer.

Ive also tryed having both classes in the same file but that produces similar results, am I missing something realy obvious or is it imposible to create this sort of 'loop' structure?

Thanks in advanced for any help :)
Advertisement
Forward declarations are what you are looking for, like this.
Okay thanks so much, especially for very quick reply :)

This topic is closed to new replies.

Advertisement