How to prevent circular #include

Discussion in 'C++' started by junw2000@gmail.com, Jan 19, 2007.

  1. Guest

    In the example below:

    ------------------------------
    f1.h

    #include "f2.h"

    class A1{

    void g();
    }

    class A2{}

    --------------------------------
    f1.cpp

    #include "f1.h"

    void A1::g(){

    B1 b;
    B1::h();
    }

    ---------------------------------
    f2.h

    #include "f1.h"
    class B1{
    static void h();
    }
    --------------------------------
    f2.cpp

    #include "f2.h"
    void B1::h(){
    A2 a;

    }

    How to solve this problem other than merging f1.h and f2.h?

    Thanks.

    Jack
     
    , Jan 19, 2007
    #1
    1. Advertising

  2. Ian Collins Guest

    wrote:
    >
    > How to solve this problem other than merging f1.h and f2.h?
    >

    Through a combination of include guards in the headers and forward
    declarations.

    --
    Ian Collins.
     
    Ian Collins, Jan 19, 2007
    #2
    1. Advertising

  3. hit_pc Guest

    On Fri, 19 Jan 2007 16:08:56 +0800, <> wrote:

    > In the example below:
    >
    > ------------------------------
    > f1.h
    >
    > #include "f2.h"
    >
    > class A1{
    >
    > void g();
    > }
    >
    > class A2{}
    >
    > --------------------------------
    > f1.cpp
    >
    > #include "f1.h"
    >
    > void A1::g(){
    >
    > B1 b;
    > B1::h();
    > }
    >
    > ---------------------------------
    > f2.h
    >
    > #include "f1.h"
    > class B1{
    > static void h();
    > }
    > --------------------------------
    > f2.cpp
    >
    > #include "f2.h"
    > void B1::h(){
    > A2 a;
    >
    > }
    >
    > How to solve this problem other than merging f1.h and f2.h?
    >
    > Thanks.
    >
    > Jack
    >

    #ifndef F2_H
    #define F2_H
    blablabla
    ……
    #endif /*F2_H*/


    --
    Hello,World!
    ----legolaskiss.
     
    hit_pc, Jan 19, 2007
    #3
  4. Grizlyk Guest

    wrote:

    > In the example below:

    This is C-language question

    a) remove "#include"s from "*.h"
    b) if can not remove, use "#ifndef/define" in each "*.h"

    /*
    LIBNAME is your libname
    FILE_NAME is current "*.h" file name
    */

    #ifndef LIBNAME##_##FILE_NAME
    #define LIBNAME##_##FILE_NAME

    > #include "f2.h"
    >
    > class A1{
    >
    > void g();
    > }
    >
    > class A2{}
    >


    #endif
     
    Grizlyk, Jan 19, 2007
    #4
    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. Danny Anderson
    Replies:
    5
    Views:
    519
    Victor Bazarov
    Aug 15, 2003
  2. Rolf Magnus
    Replies:
    2
    Views:
    619
    Karl Heinz Buchegger
    Nov 28, 2003
  3. Kiuhnm
    Replies:
    16
    Views:
    750
    Jonathan Mcdougall
    Jan 3, 2005
  4. Andreas Bogenberger
    Replies:
    3
    Views:
    967
    Andreas Bogenberger
    Feb 22, 2008
  5. Zhao Yi
    Replies:
    4
    Views:
    121
    James Coglan
    Sep 3, 2008
Loading...

Share This Page