Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > did any body know how to cross include head files?

Reply
Thread Tools

did any body know how to cross include head files?

 
 
George Zhou
Guest
Posts: n/a
 
      07-10-2003
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?



 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      07-10-2003

"George Zhou" <> wrote in message
news:bekhjm$qaj$...
> 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


 
Reply With Quote
 
 
 
 
MiniDisc_2k2
Guest
Posts: n/a
 
      07-10-2003

"George Zhou" <> wrote in message
news:bekhjm$qaj$...
> 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.



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
/* #include <someyhing.h> */ => include it or do not include it?That is the question .... Andreas Bogenberger C Programming 3 02-22-2008 10:53 AM
501 PIX "deny any any" "allow any any" Any Anybody? Networking Student Cisco 4 11-16-2006 10:40 PM
GeForce 7800 GTX Head-to-Head @ TrustedReviews Silverstrand Front Page News 0 09-12-2005 11:25 PM
Injecting code into the <head></head> section Brian W ASP .Net 10 07-02-2003 07:53 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57