473,387 Members | 1,348 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.

include each other's header files...problem

Hi,

I am now designing a program related to "graph".

In particular, I would like to have 2 objects.
#1. node

#2. branch

Nodes are connected by branches.

A single node can have multiple in/out-coming branches.

the node objects should have connected branches obj addresses.
the branch objects should have connected nodes obj addresses.

but.. that would include each other's header files...

How to get around this situation? Thanks.

Carson

Oct 7 '05 #1
4 8172
ck*******@gmail.com wrote:
Hi,

I am now designing a program related to "graph".

In particular, I would like to have 2 objects.
#1. node

#2. branch

Nodes are connected by branches.

A single node can have multiple in/out-coming branches.

the node objects should have connected branches obj addresses.
the branch objects should have connected nodes obj addresses.

but.. that would include each other's header files...

How to get around this situation? Thanks.

Carson


When two classes are as mutually dependent as this I like to put them in
the same header file.

But in any case the answer to your problem is a forward declaration.

// branch.h
class Node; // forward declaration

class Branch
{
};
// node.h
class Branch; // forward declaration

class Node
{
};

Note that with forward declarations neither header includes the other.

When a class is forward declared there are limits on what you can to
with it. Pretty much all you can do is declare a pointer to it. But that
should be enough to get your two classes declared.

john
Oct 7 '05 #2
<ck*******@gmail.com> schrieb im Newsbeitrag
news:11*********************@z14g2000cwz.googlegro ups.com...
Hi,

I am now designing a program related to "graph".

In particular, I would like to have 2 objects.
#1. node

#2. branch

Nodes are connected by branches.

A single node can have multiple in/out-coming branches.

the node objects should have connected branches obj addresses.
the branch objects should have connected nodes obj addresses.
If your member variables only need the addresses then you don't have to
include the other header file. For pointers a forward declaration of the
class is sufficient because the size is known.

but.. that would include each other's header files...

How to get around this situation? Thanks.

Carson


Greetings Chris
Oct 7 '05 #3
ck*******@gmail.com wrote:

the node objects should have connected branches obj addresses.
the branch objects should have connected nodes obj addresses.

but.. that would include each other's header files...

How to get around this situation? Thanks.


By using a forward declaration

class B; // forward declaration: somewhere there is a class called 'B'
// so whenever in the following I write 'B' this is not a typing
// error, but indeed valid, since B is a class.
//
// You can use a forward declaration in all cases, where the internals
// of B are of no importance, such as eg. defining a pointer

class A
{
....
B* TheB; // Look Ma, a pointer to B
};

class B
{
...
A* TheA;
};

--
Karl Heinz Buchegger
kb******@gascad.at
Oct 7 '05 #4
The following link describes the solution to this and other cases of
header file dependancy handling:

http://www.eventhelix.com/RealtimeMa...dePatterns.htm

--
EventStudio System Designer 2.5 - http://www.EventHelix.com/EventStudio
Sequence Diagram Based System Design and Object Modeling Tool

Oct 7 '05 #5

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

Similar topics

28
by: Steven T. Hatton | last post by:
I will assume many people reading this would never create anything similar to the example below. So let me preface this with _*IF*_ you were in a situation where you had to chose between using...
4
by: Exits Funnel | last post by:
Hello, I'm slightly confused about when to use parens around #included files and when to use angle brackets. I understand (I think) that the difference is that the compiler will search in its...
6
by: atv | last post by:
Alright, i have some questions concerning include files en global variables.I hope someone is willing to answer these. 1).Why is it that if i define a global variable in a file, say main.c, and...
60
by: Derrick Coetzee | last post by:
It seems like, in every C source file I've ever seen, there has been a very definite include order, as follows: - include system headers - include application headers - include the header...
44
by: Neil Cerutti | last post by:
In Rob Pike's style guide he urges the following: Simple rule: include files should never include include files. If instead they state (in comments or implicitly) what files they need...
14
by: Jon Rea | last post by:
I am currently cleaning up an application which was origainlly hashed together with speed of coding in mind and therefore contains quite a few "hacky" shortcuts. As part of this "revamping"...
9
by: chat | last post by:
Hi, every body. I have 3 files like this: -------------------------------------------------------- file name : header.h #ifndef TEST_H #define TEST_H int a=1; double b=0.5;
5
by: Tio | last post by:
I have project in MFC(vc++) . There are files and classes: classes:dialog1,dialog2,aaa,bbb ---------------------- main.cpp --------------------- #include "mainfrm.h" #include "dialog1.h"...
16
by: Chris Shearer Cooper | last post by:
In our Visual Studio 2005 application, we have several of our application's H files that are #included into stdafx.h. What is odd, is that when we change those application H files, VS2005...
111
by: Nate | last post by:
Hello, I am looking for a method to automatically declare variables in C. I'm not sure if there is a good way to do this, but I had something like this in mind... int i; for(i = 1; i < 4;...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.