You can put any necessary includes in header files. For example by putting
between lines 2 and 3 of "A.h". The
#include is required because the compiler requires a complete definition of class B so that it knows how much space
A will occupy and this is because you declared
anArray as an object of type
B.
Which, by the way, is wrong for what you seem to be trying to do.
For the constructor of A to work as written you need to make
anArray a
pointer to B ie. change line 7 to read
This change will make the
#include unnecessary and you can replace it with just a forward declaration of class B which you should put before line 3.
Storing a pointer to B makes it unnecessary for the compiler to know the full definition of B because the amount of space the pointer requires is independent of what it points to (at least in general).
Also, don't forget to delete
anArray, for example by creating a destructor for class A like this:
1 2 3 4
|
A::~A()
{
delete[] anArray;
}
|
And finally a
new does not return an object of the type it creates but a
pointer to the object it creates which you have to make sure is
deleted at some later point when you're done with the object it points to. So your main.cpp should be:
1 2 3 4 5
|
A* thingA = new A();
B* thingB = new B();
// do something with thingA and thingB
delete thingA;
delete thingB;
|