did any body know how to cross include head files?

Discussion in 'C++' started by George Zhou, Jul 10, 2003.

  1. George Zhou

    George Zhou Guest

    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?
    George Zhou, Jul 10, 2003
    #1
    1. Advertising

  2. "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
    John Harrison, Jul 10, 2003
    #2
    1. Advertising

  3. George Zhou

    MiniDisc_2k2 Guest

    "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.
    MiniDisc_2k2, Jul 11, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian W
    Replies:
    10
    Views:
    757
    Brian W
    Jul 2, 2003
  2. showme

    PHP and ASP.NET go HEAD to HEAD

    showme, Jul 8, 2004, in forum: ASP .Net
    Replies:
    13
    Views:
    712
    Sherif ElMetainy
    Jul 10, 2004
  3. Karl
    Replies:
    0
    Views:
    386
  4. Jiho Han

    Write into <HEAD></HEAD> section?

    Jiho Han, Jan 14, 2004, in forum: ASP .Net Building Controls
    Replies:
    6
    Views:
    223
    Sam Fields
    Jan 16, 2004
  5. Andries

    I know, I know, I don't know

    Andries, Apr 23, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    222
    Gregory Toomey
    Apr 23, 2004
Loading...

Share This Page