473,387 Members | 1,520 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Help with header files

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!

Jul 22 '05 #1
7 1347
"Daniel" <dn******@mercednet.com> wrote...
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.

[...]
Jul 22 '05 #2
Daniel wrote:
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.

Jul 22 '05 #3
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.

Jul 22 '05 #4
"Daniel" <dn******@mercednet.com> wrote...
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.
Jul 22 '05 #5
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!

Jul 22 '05 #6

"Daniel" <dn******@mercednet.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
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.
Jul 22 '05 #7
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

Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: | last post by:
I am having a major problem with file transfers - they are ending early when the bandwidth tops-out. Smaller files transfer just fine, but large files (12Mb+) can 'abort' the transfer after maybe...
3
by: dharmesh Gupta | last post by:
Hi All, i am new to header files , and have a basic question , i have made my program in three files namely, idr_bpl.cpp( contains class definitions and function definitions) bpl.cpp(...
11
by: Steven T. Hatton | last post by:
In the past there have been lengthy discussiions regarding the role of header files in C++. People have been very adamat about header files serving as in interface to the implementation. I do...
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
18
by: John Smith | last post by:
Hi all What does the group think of the practise of including one header file from inside another? I have some legacy code where this has been done, and it creates a dependency on a module...
4
by: crs2003c | last post by:
For some time now, other teachers have had this problem. Visual C++ 6.0 was once installed on a WIN98 platform and apstring.h and graphic.h worked. However, since moving to WIN2000 and WINXP,...
22
by: macAWM | last post by:
Hi list, First let me explain that my background is in Java and I am quite spoiled to its niceties (read "less ambiguous nature"). Anyway to my problems. 1. I want to write my own library for...
1
by: ligong.yang | last post by:
Hi all, I got tortured by a very weird problem when I was using k. wilder's random generator class in my program. PS: wilder's generator class can be found at...
1
by: ligong.yang | last post by:
Hi all, I got tortured by a very weird problem when I was using k. wilder's random generator class in my program. PS: wilder's generator class can be found at...
0
by: shrik | last post by:
I have following error : Total giant files in replay configuration file are : File name : /new_file/prob1.rec Given file /new_file/prob1.rec is successfully verified. Splitting for giant file...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.