C ++, Visual Studio 2013
How to connect classes correctly if you have the following structure: Base.h file:

class base{}; 

File Child1.h:

 #include "base.h" class Child : public base { } 

Child2.h file:

 #include "base.h" class Child2 : public base 

File Main.h:

 #include "Child.h" #include "Child2.h" 

With this inclusion, I get a link error: redefining the base class

    2 answers 2

    Most likely, you in h files do not have protection against double inclusion. The easiest way is to add #pragma once to the very top of each .h file or do it the old-fashioned way - wrap it in a guard "guard"

     #ifndef _FILENAME_H_ #define _FILENAME_H_ // тут код #endif 

    _FILENAME_H_ - must be unique for each file, therefore, it usually coincides with the file name. This method works on almost all available compilers with / with ++. pragma once may not work on older compilers.

    • на практически всех ... have there been cases where it did not work? - αλεχολυτ
    • Yes, I used a c compiler in which there was no define. Just was not. The compiler was tricky controllers, now I do not even remember. The author of the question is unlikely to face such a thing, but little of that ... - KoVadim
    • In this case, it cannot be called fully c компилятором . This is just some kind of hack. And by the way, the question of TS on c++ . - αλεχολυτ
    • I vkurse that a question on with ++. A fake had to use, because there was no other. And I didn't want to write code right in the codes. True, the preprocessor was then screwed up and everything worked as desired (yes, if you are very pedantic, #ifdef processing is the work of the preprocessor, not the compiler. And as a result, none of them support the correct compiler. But modern compilers hide it and do preprocessing themselves) . - KoVadim
    • It is clear that this is the work of the preprocessor, but the preprocessor directives are nevertheless described in the Language Standard. And I would refer this to the basic part of what the implementation should be able to do. Otherwise, you can come up with compilers, which, say, do not have an if . But to call this a C compiler, the language does not turn. - αλεχολυτ

    in the first line in the header

     pragma once 
    • This is already in another answer. - D-side