how to avoid using another header file inside a header file?

Discussion in 'C++' started by Newsgroup - Ann, Nov 2, 2003.

  1. I have a library function like GetOptions( ..., struct net_arch_t netArch,
    ....) and put the declaration into a regular header file like getopt.h. But
    this function declaration also needs the declaration of struct net_arch_t
    which is located in another header file, e.g., nn.h. I saw somewhere that it
    should be avoided to include another header file inside one header file. So
    I am trying to use some other methods.

    One option is for each source file that needs the GetOption function,
    include both the getopt.h and nn.h and place nn.h ahead of the getopt.h. But
    isn't it awkard? Is there any better way to do it? It happens some often to
    me. Thanks.

    Ann
    Newsgroup - Ann, Nov 2, 2003
    #1
    1. Advertising

  2. Newsgroup - Ann

    Artie Gold Guest

    Newsgroup - Ann wrote:
    > I have a library function like GetOptions( ..., struct net_arch_t netArch,
    > ...) and put the declaration into a regular header file like getopt.h. But
    > this function declaration also needs the declaration of struct net_arch_t
    > which is located in another header file, e.g., nn.h. I saw somewhere that it
    > should be avoided to include another header file inside one header file. So
    > I am trying to use some other methods.


    A header should always #include whatever it needs. Perhaps there was
    some specific context in which this suggestion was made?

    >
    > One option is for each source file that needs the GetOption function,
    > include both the getopt.h and nn.h and place nn.h ahead of the getopt.h. But
    > isn't it awkard? Is there any better way to do it? It happens some often to
    > me. Thanks.
    >

    See above.

    HTH,
    --ag




    --
    Artie Gold -- Austin, Texas
    Oh, for the good old days of regular old SPAM.
    Artie Gold, Nov 2, 2003
    #2
    1. Advertising

  3. Newsgroup - Ann

    Phlip Guest

    Newsgroup - Ann wrote:

    > I have a library function like GetOptions( ..., struct net_arch_t netArch,
    > ...) and put the declaration into a regular header file like getopt.h. But
    > this function declaration also needs the declaration of struct net_arch_t
    > which is located in another header file, e.g., nn.h. I saw somewhere that

    it
    > should be avoided to include another header file inside one header file.

    So
    > I am trying to use some other methods.
    >
    > One option is for each source file that needs the GetOption function,
    > include both the getopt.h and nn.h and place nn.h ahead of the getopt.h.

    But
    > isn't it awkard? Is there any better way to do it? It happens some often

    to
    > me. Thanks.


    Forward declare:

    struct net_arch_t;

    GetOptions(int argc, char **argv, net_arch_t & arch);

    --
    Phlip
    Phlip, Nov 2, 2003
    #3
  4. Newsgroup - Ann

    Moonlit Guest

    Hi,

    "Newsgroup - Ann" <> wrote in message
    news:3fa48556$...
    > I have a library function like GetOptions( ..., struct net_arch_t netArch,
    > ...) and put the declaration into a regular header file like getopt.h. But
    > this function declaration also needs the declaration of struct net_arch_t
    > which is located in another header file, e.g., nn.h. I saw somewhere that

    it
    > should be avoided to include another header file inside one header file.

    So
    > I am trying to use some other methods.


    No. You should include all header files that are needed. Otherwise someone
    using your module has to understand he not only has to include the header
    file of your module (which is opvious) but he also has to figure out what
    are header files should be included to make it work.

    Regards, Ron AF Greve.


    >
    > One option is for each source file that needs the GetOption function,
    > include both the getopt.h and nn.h and place nn.h ahead of the getopt.h.

    But
    > isn't it awkard? Is there any better way to do it? It happens some often

    to
    > me. Thanks.
    >
    > Ann
    >
    >
    Moonlit, Nov 2, 2003
    #4
  5. Newsgroup - Ann

    lilburne Guest

    Newsgroup - Ann wrote:
    > I have a library function like GetOptions( ..., struct net_arch_t netArch,
    > ...) and put the declaration into a regular header file like getopt.h. But
    > this function declaration also needs the declaration of struct net_arch_t
    > which is located in another header file, e.g., nn.h. I saw somewhere that it
    > should be avoided to include another header file inside one header file. So
    > I am trying to use some other methods.
    >
    > One option is for each source file that needs the GetOption function,
    > include both the getopt.h and nn.h and place nn.h ahead of the getopt.h. But
    > isn't it awkard? Is there any better way to do it? It happens some often to
    > me. Thanks.
    >


    You can use forward declarations like:

    class myClass;
    struct someStruct;

    but this only works if all the usages of myClass and
    someStruct in the header are via references or pointers. In
    the example you give of GetOptions netArch is being passed
    by value so that definition at least will have to any source
    code that calls the function. Some compilers will let you
    get away with just the forward declaration of struct
    net_arch_t in the header others will not.

    In general you should keep includes to a minimum in header
    files, include only those headers you need and no more. One
    recommendation is that the first include in classA.cpp
    should be classA.h this will ensure that classA.h is complete.

    What you shouldn't do is put any additional includes into
    classA.h that are required internally by classA.cpp. Example
    classA::sort() uses classX internally, but there is no usage
    of classX in the classA interface, do not include classX in
    classA.h but in classA.cpp.
    lilburne, Nov 2, 2003
    #5
    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. nail
    Replies:
    0
    Views:
    1,539
  2. Alexander Malkis
    Replies:
    8
    Views:
    517
    Alexander Malkis
    Apr 14, 2004
  3. Roger23
    Replies:
    2
    Views:
    997
    Roger23
    Oct 12, 2006
  4. Kiran More
    Replies:
    2
    Views:
    314
    Kiran More
    Nov 14, 2006
  5. 123Jim
    Replies:
    0
    Views:
    140
    123Jim
    May 16, 2011
Loading...

Share This Page