Error LNK2005: already defined in.obj

Please tell me how to overcome the problem?

error LNK2005: already defined in .obj

I can't say that I haven't tried both #pragma once and #infndef and together, I can't overcome this problem in any way.
I have a file that contains the compare and delete functions for classes .. This is done so that in another class, you can simply declare a pointer to a function and pass the desired 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, you need to include this file in each of the classes. I tried to divide this file into a separate header and sortsevy, included this file in the lowest class, and then included the lower class in the hierarchy up. Nothing helps, that is, the files themselves are compiled into object files normally, but the linking does not take place. Help please. I don't know what to think of even ...

Author: alex-rudenkiy, 2013-10-26

2 answers

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
};
 2
Author: nitrocaster, 2013-10-26 10:26:03

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

Judging by

#ifndef HLPFUNC_H
#define HLPFUNC_H

All this is located in a certain hlpfunc. h, which in turn is included in several *. cpp. Would have been .c/.cpp the file was one the problem would not have shown up.

That's the crux of the problem. The compiler converts each .c/.cpp file in. obj independently at the same time, it does not pay attention to other files - they are also connected independently (from the neighboring. obj).

How do different .h and. get there?hpp? And simply - with the help of #include "...". Moreover, if you do not go into details, this is a simple insertion of text from the plug-in file into ours .c/.cpp file. The insertion is recursive and to avoid adding duplicates, the compiler (using the preprocessor) monitors #ifndef and #pragma once. So inside one .obj will not repeat implementations. But each. obj will have its own copy of hlpfunc. h and accordingly linker these repeats it will find and issue error LNK2005.

How to fix everything here. Move the implementation of the functions to a separate. cpp file (don't 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); 
}
....
 2
Author: mr NAE, 2016-12-29 12:06:46