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

  • Thread starter Newsgroup - Ann
  • Start date
N

Newsgroup - Ann

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
 
A

Artie Gold

Newsgroup said:
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
 
P

Phlip

Newsgroup said:
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);
 
M

Moonlit

Hi,

Newsgroup - Ann said:
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.
 
L

lilburne

Newsgroup said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top