Header File Issue

A

Adam Hartshorne

Hi All,

I am a relative newbie to c++, and at the moment am trying to achieve
the following.

For a particular reason I have two classes, and within each I have a
variable of type "pointer to other class". Therefore I need to #include
"Class B" in the .h file of Class A and vice versa. However as soon as I
do this I get a mass of compiler errors, which if I take out one or the
other includes and comment out the pointer variable disappear.

Therefore I assume that it is something to do with the including of the
header files that I am doing wrong.

Adam
 
K

Karl Heinz Buchegger

Adam said:
Hi All,

I am a relative newbie to c++, and at the moment am trying to achieve
the following.

For a particular reason I have two classes, and within each I have a
variable of type "pointer to other class". Therefore I need to #include
"Class B" in the .h file of Class A and vice versa. However as soon as I
do this I get a mass of compiler errors, which if I take out one or the
other includes and comment out the pointer variable disappear.

Therefore I assume that it is something to do with the including of the
header files that I am doing wrong.

No it has something to do with you not reading the error messages.
They are there for a reason and you should at least read the first
one or two.

But to answer your next question:
If all you do is to declare pointers, you can get away
with a 'forward declaration'. This is a declaration that
tells the compiler that something exists, but doesn't talk
about the details.
That's all that is needed to declare a pointer:

a.h
***

class b; // forward declare b
// it tells the compiler, that indeed somewhere
// there exists a class called 'b' and the usage
// of b in the further is not a simple typing error
class a
{
b* m_MyB;
};

b.h
***

class a; // forward declare a
class b
{
a* m_MyA;
};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,661
Latest member
FloridaHan

Latest Threads

Top