473,322 Members | 1,379 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,322 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 6738
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
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.