1

So i have a main source file and a my class header file and my class source file
main source file includes my class header file and everything works fine.
Now i want to add string library to my class header file but it is already
included in main source file i am unsure if it will get included twice since.
Main source has include string my class header file has include string will it get included more include string.

My class source file has included more then once?

3
  • 1
    You explicitly include what you need. Read up on header guards and pragmas. If it is included in some header file then it need not be included in the source file. Eventually in the end, it all becomes one file when the preprocessor stitches it all together. Commented Jun 19, 2018 at 9:50
  • If you are talking about #include <string>, all will be well. Standard headers better be written with production quality. Commented Jun 19, 2018 at 9:52
  • yes #include <string> Commented Jun 19, 2018 at 9:53

1 Answer 1

3

as Ron and StoryTeller pointed out, you want to use a "header guard" in your header file:

#ifndef YOURMODULENAME_YOURHEADERNAME_H_MAYBESOMESALT
#define YOURMODULENAME_YOURHEADERNAME_H_MAYBESOMESALT

// here you can put code (and maybe other includes) 
// that will only be included ONCE for the whole project

#endif //YOURMODULENAME_YOURHEADERNAME_H_MAYBESOMESALT

Now you can safely include your header mutliple times in your CPP files.

All standard libraries have done so, you could actually do this in your header / cpp files, and it will be used only ONCE:

#include <string>
#include <string>
// ... as often as you like ...
#include <string>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.