how does header including in c++ work? I have the classes already implemented in .h file and when there is #include in two files, there’s this error:
files.h:14:7: error: redefinition of ‘class abstract_file’
files.h:14:20: error: previous definition of ‘class abstract_file’`
multiple times for each class and enum.
Can anyone explain this?
You can only include a definition one time but headers can be included multiple times. To fix that, add:
to the top of each header file.
While
#pragma onceis relatively common, if you are using an older compiler it may not be supported. In that case, you need to fall back on manual include guards:(note that you need to replace
MY_HEADER_Hwith a unique string for each header file)