By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,827 Members | 813 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,827 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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.

Jul 19 '05 #2

P: n/a
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
Jul 19 '05 #3

P: n/a
Hi,

"Newsgroup - Ann" <ne******@yahoo.com> wrote in message
news:3f**********@rcfnews.cs.umass.edu...
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

Jul 19 '05 #4

P: n/a
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.

Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.