How to include several classes with the same inclusion in it?

How to include two .h / class files that have #include "h_file.h"? both require a .h file definition, and both are included in the main program. how can I prevent overriding .h definitions (which causes the compiler not to compile)?

I have:

main.cpp
class1.h/class1.cpp
class2.h/class2.cpp
h_file.h
+3
source share
1 answer

Use include guard:

#ifndef INCLUDE_GUARD_IDENTIFIER_GOES_HERE
#define INCLUDE_GUARD_IDENTIFIER_GOES_HERE

// code for header

#endif

The second time it turned on, it is actually an empty file.


There are many different ways to select an identifier INCLUDE_GUARD_IDENTIFIER_GOES_HERE, with justifications for each. Personally, I do FILE_DIRECTORY_FILE_NAME_CLASS/FUNCTION_NAME_HPP:

#ifndef UTILITY_FOO_HPP
#define UTILITY_FOO_HPP

namespace utility
{
    void foo();
}

#endif

Others will generate a GUID and attach them to the base name, for example:

INCLUDE_GUARD_A629F54A136C49C9938CB33EF8EDE676

, . , . , , , , : , .

+9

Source: https://habr.com/ru/post/1773738/


All Articles