#### Archived

This topic is now archived and is closed to further replies.

This topic is 5468 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi there, I am quite new to C++ and have come across a problem with header files... I have two classes defined in the same header file. The problem is that one class relies on objects from the other and vice versa -- this is throwing lots of compiler errors. I have also tried to place them in seperate header files but the same problem occurs. How can i solve this? Thanks in advance, MrWendell

##### Share on other sites
i think you need to forward declare (is that the right terminology) your classes: ie

    class A;class B;class A {  public: B classAsB;}class B { public: A classBsA;}

since you declare class A and B before their actual definitions, then you should have no problems . . . someone correct me if im wrong (oops you dont really have to ask that here)

[edited by - mstein on March 6, 2003 2:51:10 PM]

##### Share on other sites
Well you can do one of two things.

1. Try to restructure it so they do not depend on each other. Now, this isn''t always feasible, so that leads us to the other option.

2. Use forward declarations:

//B.h
class A;
class B
{
...
};

//B.cpp
#include "B.h"
#include "A.h" //make good on fwd declaration

//A.h
class B;
class A
{
...
};

//A.cpp
#include "A.h"
#include "B.h" //ditto