someprogram.cpp -----------> #include<string>
     |
     | #include"someheader.h"
     V
someheader.h --------------> #include<string>

what happens if I do this? Is it necessary to include the headers into all files that use them, or can you just go

#include<string>
#include...
#include"someheader.h"
#include"anotherheader.h"

and string and other required headers will be included in the ones below it as well?

Recommended Answers

All 11 Replies

You can derive the answer yourself from the fact that #include effectively copies the text of the header file into the cpp file before the compiler starts compiling.

So, my question is: why would you want to require people to follow extra instructions just to use your header file?

well, I knew that the include() function in PHP just copied the text from one file into the calling file, but I didn't know if C++ would make the included functions, classes, etc. available only to the scope of the including file.
Is there an issue with including a header in a main module and also in another header? Should I be using #ifndef, etc. directives in all my headers to avoid complications with this?

Yes, guard all your headers with #ifndef directives.

And your origional question seemed to be about whether or not you could include <string> multiples: well all the standard headers use #ifndef too! Cool, right?

Cool, right?

Wrong.

Muhahahahahahah! C++ is never cool.

For the sake of information, are you saying I am wrong about headers, or just spouting your opinion on C++?

For the sake of information, are you saying I am wrong about headers, or just spouting your opinion on C++?

Just for the sake of information, are you just refusing to recognize my authority on the coolness of things, or just feeling butthurt about some nonsensical post on the internet?

I was just wondering what your "wrong" comment was in response to. If you were saying that standard c++ headers in fact do not use #ifndef statements, that would be important. I see that is not the case.

in the source file..u can just go..

#include<string>
#include...
#include"someheader.h"
#include"anotherheader.h"

and remove #include<string> in the "someheader.h"

in the source file..u can just go..

#include<string>
#include...
#include"someheader.h"
#include"anotherheader.h"

and remove #include<string> in the "someheader.h"

If you had read any of the previous posts, you would have seen this point:

So, my question is: why would you want to require people to follow extra instructions just to use your header file?

If you had read any of the previous posts, you would have seen this point:

I don't have problems with that..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.