-1

I have 2 objects that use std::string in order to work. Object A may be used independently from object B. However, a situation might arise where both object A and B might be used in the same main.cpp file which might cause header collision and the header might get compiled twice. How do I prevent from being compiled twice? I've tried using:

#ifndef string_H
#define string_H
#endif

It does not work and object B is unable to use the string library, I think this is meant to be used only for third-party libraries such as my own and not for official libraries. So my question is, how would you put include guards over a header from a standard library?

EDIT: I am using Visual Studio 2017 so I believe it's all written by Microsoft. Thank you for your answers, I was not aware that std:: has include guards already built-in.

1 Answers1

0

how would you put include guards over a header from a standard library

You wouldn't. Standard library headers already have include guards built into them.
Edit:
The C++ standard requires that standard library headers can't be double included, but it doesn't specify how an implementation should go about doing so. However, any standard compliant implementation will allow you to include standard library headers multiple times without causing any issues.
(source)

Pierce Griffiths
  • 733
  • 3
  • 15