If A contains an instance of B, it must know all about B. Therefore B must come first (or included from a header).
B however doesn't contain an instance of A (it couldn't). Instead it just has to know that a class by the name A exists, so it can declare pointers to it. For that forward declarations are used.
All in all the layout would be:
Code:
//forward declaration of A
class A;
//declaration of B
class B
{
//...
};
//declaration of A
class A
{
//we know all about B:
B b;
//...
};
//now we also know all about A and can implement the function in B
//that needs to know about A's member functions
void B::set_pointer(A* a) {...}
Other notes:
main() should have an explicit return type (C++ does not accept defaulting return types).
A little indentation wouldn't hurt.
This, of course, is the same as
except potentially calling one constructor and one destructor less.