Archived

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

MrWendell1982

C++ header files

Recommended Posts

MrWendell1982    122
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 this post


Link to post
Share on other sites
mstein    122
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 this post


Link to post
Share on other sites
antareus    576
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

Share this post


Link to post
Share on other sites