473,397 Members | 2,084 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,397 software developers and data experts.

Header files problem ... recursive includes

3
I have 8 header files in a cpp project, and some cross refer to each other:

ie
Expand|Select|Wrap|Line Numbers
  1. #include "Observer.h" in file "Subject.h"
  2.  
  3.  
  4. #include "Subject.h" in file "Observer.h"
  5.  
and the compiler is getting into an infinite recursive loop with these includes.

Is there any command options i can use to specify that a file should only be included once?

Thanks
Jul 25 '10 #1
7 6760
weaknessforcats
9,208 Expert Mod 8TB
I recommend using an inclusion guard iin each of your hesder files:

Expand|Select|Wrap|Line Numbers
  1. #ifdef SUBJECTH
  2. #define SUBJECTH
  3.  
  4. ...contents of subject.h gl here
  5.  
  6. #endif
The first time the file is included SUBJECTH is not defined. It is immediately defined followed by the contents of the header file.

On subsequent includes, SUBJECTH is defined so the preprocessor skips to the #endif thereby not including the file again.
Jul 25 '10 #2
Banfa
9,065 Expert Mod 8TB
Once you have added inclusion guards you may well find the code not compiling because of undefined symbols on your classes.

class A can not use to class B in its definition if class B uses class A in its definition.

However class A can use a reference(or pointer) to class B in its definition even if class B uses class A in its definition.

If can do this using a forward declaration. A forward declaration looks something like this

Expand|Select|Wrap|Line Numbers
  1. class B;
  2.  
It tells the compiler that class B is going to be defined at some future time. This is enough to allow the compiler to create pointers and references to class B but not enough to access the members of class B.

In the situation I have described the forward declaration to B would be put in the class A header and the class A header would just be included into the class B header.
Jul 25 '10 #3
donbock
2,426 Expert 2GB
There is a typo in weaknessforcat's code snippet. It should be ifndef rather than ifdef.
Expand|Select|Wrap|Line Numbers
  1. #ifndef SUBJECTH
  2. #define SUBJECTH
  3. ...
  4. #endif
An inclusion guard of this sort is a common idiom among C/C++ programmers.

One way to handle interdependencies such as those described by banfa is to put all of your forward declarations, incomplete structure declarations, and related stuff in a third header file that is included by both of the other headers. All three header files have their own inclusion guards. The third header is only included by other header files, never by a cpp file.
Jul 25 '10 #4
weaknessforcats
9,208 Expert Mod 8TB
It should be ifndef rather than ifdef.
Oops..........
Jul 26 '10 #5
Banfa
9,065 Expert Mod 8TB
Oops..........
That's C++ for you :D
Jul 26 '10 #6
donbock
2,426 Expert 2GB
One last point about this inclusion guard idiom: it is crucial that a unique macro name be used for each header file's inclusion guard. The inclusion guard will malfunction badly if that macro name happens to already be defined for some other purpose.

Personally, I try to achieve unique names by choosing macro names that match the header filename in capital letters, replacing the period with an underscore. I'm then careful to not define macros of the form foo_H for anything else.
Jul 27 '10 #7
hype261
207 100+
You can also use #pragma once instead of the #include guards if your compilier supports it. It is non-standard which limits portability of your code.
Jul 27 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Mike Meyer | last post by:
I've got a package that includes an extension that has a number of header files in the directory with the extension. They are specified as "depends = " in the Extension class. However, Distutils...
3
by: Chris Mantoulidis | last post by:
Seperate compilation (that's what it's called, right?) seems to be quite popular, so I decided to get some info about it, and (d'oh) use it... But it's whole structure seems weird to me... ...
7
by: header_question | last post by:
Is it best to place all #include files in one header file seperate from all the other files in a project? What is the logic that determines the order of the #include files, does it depend on the...
16
by: matthurne | last post by:
I just started learning C++ on my own...I'm using Accelerated C++. Something it hasn't explained and I keep wondering about is how header files actually work. I suspect it doesn't get into it...
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...
18
by: Exits Funnel | last post by:
Hello, I'm a little confused about where I should include header files and was wondering whether there was some convention. Imagine I've written a class foo and put the definition in foo.h and...
3
by: pooja | last post by:
Suppose i have created a class c1 with f1()in c1.cpp and included this c1.cpp in file1.cpp file , which is also having main() by giving the statement #include "c1.cpp". the same i can do by...
3
by: Jared | last post by:
Hi, Can someone explain the basic structure of a header file? I just completed a introductory C programming class. The course stressed structured programming and creating reusable code. We...
15
by: Simon Cooke | last post by:
Does anyone know of any tools for refactoring header files? We're using a third party codebase at work, and pretty much every file includes a 50Mb precompiled header file. I'm looking for a tool...
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...
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...
0
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
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,...
0
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...

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.