Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LNK 2005 in Visual C++ in Visual Studio 2010

I'm trying to compile a C++ program, written using Visual C++ 2005 and MFC, in MS VS 2010. Sadly I'm getting the following error during compilation:

Error 2 error LNK2005: "public: virtual __thiscall CMemDC::~CMemDC(void)" (??1CMemDC@@UAE@XZ) already defined in CMemDCImpl.obj Project\Project\Project\uafxcwd.lib(afxglobals.obj) Project.

CMemDCImpl has a header file which contains definitions of all members of the class CMemDCImpl, and *.cpp file with their implementations. Please help me to fix this error.

like image 786
Andrey Avatar asked Feb 20 '11 17:02

Andrey


2 Answers

You mention that you CMemDCImpl is defined in a cpp file. However, it also seems to be defined in uafxcwd.lib (a library you apparently use). I can think of two possibilities for this error:

  1. You're including the cpp of the library you're attempting to use. Usually, when you use a precompiled library, you only need to reference the header file in your own source file and the library at link time. Is it possible that you included the source .cpp files of the library in your own project? If this is the case, simply remove the source .cpp files from your project.
  2. You're defining a class of your own that has the same name as the one you're referencing in the library and you have a name clash. The preferred method to fix this issue is to encapsulate the class you defined yourself in a namespace:

.

namespace Foo
{
    class CMemDC
    {
        // ...
    };
}

// Usage:
Foo::CMemDC myMemDC;
like image 138
joce Avatar answered Sep 25 '22 15:09

joce


Without the actual code, we can only guess. Most likely you have done one of these:

  • Implemented CMemDC::~CMemDC() {} twice, maybe a copy-paste that you didn't rename to CMemDCImpl::~CMemDCImpl()
  • Implemented CMemDC::~CMemDC() in a header file after CMemDC class definition instead of in the class definition
like image 24
Erik Avatar answered Sep 26 '22 15:09

Erik