473,480 Members | 2,077 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

did any body know how to cross include head files?

Such as in a.h, I need put #include "b.h", while in b.h, I need put #include
"a.h", but it does not work! Any alternative way to do it?

Jul 19 '05 #1
2 9358

"George Zhou" <zh****@udel.edu> wrote in message
news:be**********@news.udel.edu...
Such as in a.h, I need put #include "b.h", while in b.h, I need put #include "a.h", but it does not work! Any alternative way to do it?


Include guards.

#ifndef A_H
#define A_H

// this is a.h

#endif

#ifndef B_H
#define B_H

// this is b.h

#endif

Might not be what you need, but its the answer to your question.

john
Jul 19 '05 #2

"George Zhou" <zh****@udel.edu> wrote in message
news:be**********@news.udel.edu...
Such as in a.h, I need put #include "b.h", while in b.h, I need put #include "a.h", but it does not work! Any alternative way to do it?


I've had this problem before. Something like this:

// a.h
#include "b.h"
class A
{
public:
A();
A(B& data);
int var;
};
// b.h
#include "a.h"
class B
{
public:
B();
B(A& data);
int var;
};

As you can see, both classes need each other's files. You cannot do this,
either you'll be using an endless loop or you'll be smart and use what John
told you to do. Either way, however, one of the files isn't going to have
access to the other's data. So what you need to do is declare, but not
define the information:

// a.h
class B; // declaration

class A
{
public:
A();
A(B& data);
int var;
};

// b.h
#include "a.h"

class B
{
public:
B();
B(A& data);
int var;
};

This will work. I always put a

#pragma once

in my header files, but not all compilers support that. They function
exactly the same way as what John told you to do, it's just less coding.
It's a good idea, it'll protect you from including files more than once (and
then getting a billion redefinition errors).

--
MiniDisc_2k2
To reply, replace nospam.com with cox dot net.

Jul 19 '05 #3

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

Similar topics

2
7658
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
23
3036
by: Big Bill | last post by:
http://www.promcars.co.uk/pages/bonnie.php I don't believe they should be there, can I take them out without stopping the includes from functioning? I'm the (hapless) optimiser on this one... I...
21
2599
by: Chen ShuSheng | last post by:
Hey, In head file 'stdio.h', I only find the prototype of these two function but no body. Could someone pls tell me where their bodies are ? -------------- Life is magical.
6
2504
by: Shawn | last post by:
Hello: I have the following code in a PHP file. An HTML form passes user comment data to the PHP, which then appends the user comments to the end of the HTML file on which the form is located....
2
6334
by: akhilesh.noida | last post by:
I am trying to compile glibc-2.5 for ARM based board. But I am getting errors while configuring it. Please check and give your inputs for resolving this. configure command : $...
6
2538
by: _Who | last post by:
I use the code below to change to a style sheet that has: body { ....
3
2991
by: Sunny | last post by:
Hi, Can someone tell me, How to load the Body Html from a text file that contains javascript. to Manage my files I am creating an Index Page. <html> <head> <meta http-equiv="content-type"...
2
1801
by: urbanedge | last post by:
I've just acquired a site and uploaded to godaddy and the email function won't work. I'm new to php and I'm not able to determine where the issue is. I've commented out $this->header .=...
0
7051
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
6915
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
7054
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,...
1
6750
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...
0
6993
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5353
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4794
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
1307
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.