Help with header files

D

Daniel

I think this question will be really easy or really hard to answer.

I have two classes, TestAction and TestObject. Each TestObject has a
pointer to a TestAction, and vice versa. So, both header files need the
#ifndef compiler directives so there will not be an infinite loop of
text substitutions. The problem is, when I use the #ifndef directives,
my code won't compile, and if I take them out, my compiler crashes
because it tries to infinitely include the header files.

Here's the code; it's really simple:

(TestObject.h)

#ifndef TESTOBJECT_H
#define TESTOBJECT_H

#include "TestAction.h"

class TestObject
{
TestAction * tAction;
};

#endif

(TestAction.h)

#ifndef TESTACTION_H
#define TESTACTION_H

#include "TestObject.h"

class TestAction
{
TestObject * tObject;
};

#endif

(TestObject.cpp)

#include "TestObject.h"

(TestAction.cpp)

#include "TestAction.h"

(End)

If it makes a difference, I'm using the beta version of Visual C++
2005. Also, these are just test files to explore the problem -- I
encountered this problem in another project and wrote these test files
as an example of what won't work.

Thanks for the help!
 
E

E. Robert Tisdale

Daniel said:
I think this question will be really easy or really hard to answer.

I have two classes, TestAction and TestObject. Each TestObject has a
pointer to a TestAction, and vice versa. So, both header files need the
#ifndef compiler directives so there will not be an infinite loop of
text substitutions. The problem is, when I use the #ifndef directives,
my code won't compile, and if I take them out, my compiler crashes
because it tries to infinitely include the header files.

Here's the code; it's really simple:

(TestObject.h):

#ifndef TESTOBJECT_H
#define TESTOBJECT_H 1

#include "TestAction.h"

class TestAction; // forward declaration
class TestObject {
TestAction* tAction;
};

#endif//TESTOBJECT_H

(TestAction.h):

#ifndef TESTACTION_H
#define TESTACTION_H 1

#include "TestObject.h"

class TestObject; // forward declaration
class TestAction {
TestObject* tObject;
};

#endif//TESTACTION_H

(TestObject.cpp):

#include "TestObject.h"

(TestAction.cpp):

#include "TestAction.h"


If it makes a difference,

It doesn't.
I'm using the beta version of Visual C++ 2005.
Also, these are just test files to explore the problem --
I encountered this problem in another project
and wrote these test files as an example of what won't work.

Thanks for the help!

Just include the above forward declarations
and it should work just fine.
 
V

Victor Bazarov

Daniel said:
I think this question will be really easy or really hard to answer.

I have two classes, TestAction and TestObject. Each TestObject has a
pointer to a TestAction, and vice versa. So, both header files need the
#ifndef compiler directives so there will not be an infinite loop of
text substitutions. The problem is, when I use the #ifndef directives,
my code won't compile, and if I take them out, my compiler crashes
because it tries to infinitely include the header files.

Look up "forward declarations" in the FAQ.

[...]
 
D

Daniel

Is that all?!

I didn't know one was expected to put in forward declarations like that
-- I though the whole point of including the header file was that it
already has the needed declarations in it.
Am I missing something?

Anyway, thanks, I'll go try that.
 
V

Victor Bazarov

Daniel said:
Is that all?!

I didn't know one was expected to put in forward declarations like that
-- I though the whole point of including the header file was that it
already has the needed declarations in it.
Am I missing something?

No, not really. But we live in an imperfect world. There are
problems that have to be solved with work-arounds. A forward
declaration is a work-around.
 
D

Daniel

Cool. I can accept imperfection--I just wanted to make sure I wasn't
totally off my rocker.

So I have to include forward declarations AND the header files.
Gotcha.

Thanks, guys!
 
C

Covec

Daniel said:
Cool. I can accept imperfection--I just wanted to make sure I wasn't
totally off my rocker.

So I have to include forward declarations AND the header files.
Gotcha.

No, you don't include the header files in the opposite header files. You
just use forward declarations, and as long as you're only declaring pointers
to the forwardly-declared classes, you'll be fine. Only include the .h
files in the opposite .cpp files.
 
J

Joseph Turian

Victor,
No, not really. But we live in an imperfect world. There are
problems that have to be solved with work-arounds. A forward
declaration is a work-around.

I don't think forward declarations are merely a work-around, since they
are useful in themselves for reducing dependencies by pruning the
include list.

Joseph
 

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,199
Messages
2,571,045
Members
47,643
Latest member
ashutoshjha_1101

Latest Threads

Top