Please tell me how to overcome the problem?

error LNK2005: already defined in .obj 

I can’t already, that I haven’t tried both #pragma once and #infndef and together, I just can’t beat this problem.
I have a file in which the compare and delete functions for the classes are located. This is done to simply declare a function pointer in another class and pass the necessary function there. But in this file you need to use the declared classes. That is, cross-inclusion.
Here is the file

 #ifndef HLPFUNC_H #define HLPFUNC_H #include "departament.h" #include "brand.h" #include "model.h" #include "quality.h" #include "parts.h" #include "reptype.h" #include "pointerArray.h" void delDprt(void *delEl) { delete (departament*)(delEl); } void delBrnd(void *delEl) { delete (brand*)(delEl); } void delMdl(void *delEl) { delete (model*)(delEl); } void delRprTp(void *delEl) { delete (reptype*)(delEl); } void delPrts(void *delEl) { delete (parts*)(delEl); } void delQlty(void *delEl) { delete (quality*)(delEl); } int cmpDprt(void *p, string key) { if (((departament*)p)->name == key) { return 0; } else { if (((departament*)p)->name > key) { return 1; } else {return -1;} } } int cmpBrand(void *p, string key) { if (((brand*)p)->name == key) { return 0; } else { if (((brand*)p)->name > key) { return 1; } else {return -1;} } } int cmpModel(void *p, string key) { if (((model*)p)->name == key) { return 0; } else { if (((model*)p)->name > key) { return 1; } else {return -1;} } } int cmpRprType(void *p, string key) { if (((reptype*)p)->name == key) { return 0; } else { if (((reptype*)p)->name > key) { return 1; } else {return -1;} } } int cmpParts(void *p, string key) { if (((parts*)p)->name == key) { return 0; } else { if (((parts*)p)->name > key) { return 1; } else {return -1;} } } int cmpQlty(void *p, string key) { if (((quality*)p)->qlt == key) { return 0; } else { if (((quality*)p)->qlt > key) { return 1; } else {return -1;} } } #endif 

Accordingly, in each of the classes you need to include this file. I tried to divide this file into separate header and header, included this file in the lowest class, and then included in the hierarchy upward the lower class. Nothing helps, that is, the files themselves are compiled into object files normally, but the linking does not go away. Help me please. I do not know what to think of even ...

    2 answers 2

    Use forward declaration.

    Foo.hpp:

     class Bar; class Foo { public: Foo(Bar const& bar) { ... } }; 

    Bar.hpp:

     class Foo; class Bar { public: Bar(Foo const& foo) { ... } }; 

    main.cpp:

     #include "Foo.hpp" #include "Bar.hpp" int main() { // делаем что-то с Foo и Bar }; 
    • Nothing happens. Did not understand a little, it only works for classes? I need to associate a regular file with functions with a class and cross. - CROSP

    I am sure that in three years the author of the question himself figured out what was happening, but it’s still a pity to leave the question unanswered.

    Judging by

     #ifndef HLPFUNC_H #define HLPFUNC_H 

    All this is in a certain hlpfunc.h, which in turn is included in several * .cpp. If there was a .c / .cpp file, there would be one problem that did not appear.

    That is the essence of the problem. The compiler converts each .c / .cpp file to .obj independently while not paying attention to other files - they also connect independently (from the neighboring .obj).

    How do different .h and .hpp get there? And just by using #include "..." . Moreover, if you do not go into details, it is simple to insert text from the included file into our .c / .cpp file. Insertion occurs recursively and in order not to add duplicates the compiler (using the preprocessor) monitors #ifndef and #pragma once . Therefore, inside one .obj there will be no repetitions of implementations. But each .obj will have its own copy of hlpfunc.h and, accordingly, the linker will find and repeat these error LNK2005 .

    How to fix it here. Transfer the implementation of functions to a separate .cpp file (do not forget to add it to the project :)) and leave only the definitions in the header file.

    hlpfunc.h:

     .... void delDprt(void *delEl); void delBrnd(void *delEl); .... 

    hlpfunc.c:

     #include "hlpfunc.h" .... void delDprt(void *delEl) { delete (departament*)(delEl); } void delBrnd(void *delEl) { delete (brand*)(delEl); } .... 
    • In addition to this excellent answer, it sometimes happens that some programmers manage to insert into the "header" not the definition, but the implementation. I take into account that this issue (with minor changes) I see in a week already the third time, I would not exclude such a source of the linking problem. - Alexander Muksimov
    • Absolutely agree, @AlexanderMuksimov. I didn’t send to paint how it can be used, but here’s an example: I happened to put in order one project in which only main.cpp was compiled, and all other * .cpp were connected to it #include "... cpp". And it was a working draft! - mr NAE