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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Newsgroup - Ann

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

    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


  • Artie Gold

    #2
    Re: how to avoid using another header file inside a header file?

    Newsgroup - Ann wrote:[color=blue]
    > 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.[/color]

    A header should always #include whatever it needs. Perhaps there was
    some specific context in which this suggestion was made?
    [color=blue]
    >
    > 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.
    >[/color]
    See above.

    HTH,
    --ag




    --
    Artie Gold -- Austin, Texas
    Oh, for the good old days of regular old SPAM.

    Comment

    • Phlip

      #3
      Re: how to avoid using another header file inside a header file?

      Newsgroup - Ann wrote:
      [color=blue]
      > 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[/color]
      it[color=blue]
      > should be avoided to include another header file inside one header file.[/color]
      So[color=blue]
      > 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.[/color]
      But[color=blue]
      > isn't it awkard? Is there any better way to do it? It happens some often[/color]
      to[color=blue]
      > me. Thanks.[/color]

      Forward declare:

      struct net_arch_t;

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

      --
      Phlip


      Comment

      • Moonlit

        #4
        Re: how to avoid using another header file inside a header file?

        Hi,

        "Newsgroup - Ann" <news_ann@yahoo .com> wrote in message
        news:3fa48556$1 _1@rcfnews.cs.u mass.edu...[color=blue]
        > 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[/color]
        it[color=blue]
        > should be avoided to include another header file inside one header file.[/color]
        So[color=blue]
        > I am trying to use some other methods.[/color]

        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.

        [color=blue]
        >
        > 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.[/color]
        But[color=blue]
        > isn't it awkard? Is there any better way to do it? It happens some often[/color]
        to[color=blue]
        > me. Thanks.
        >
        > Ann
        >
        >[/color]


        Comment

        • lilburne

          #5
          Re: how to avoid using another header file inside a header file?

          Newsgroup - Ann wrote:[color=blue]
          > 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.
          >[/color]

          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.

          Comment

          Working...