MFC: linking error

Dear experts,

My application worked fine, but suddenly started giving me

5>nafxcw.lib(afxglobals.obj) : error LNK2005: "public: virtual __thiscall CMemDC::~CMemDC(void)" (??1CMemDC@@UAE@XZ) already defined in WaveFormDialog.obj
5>.\Release/Book.exe : fatal error LNK1169: one or more multiply defined symbols found

I didn't do any changes at all. Just rebuilt the project. Could you please tell me how I could fix that?
CMemDC::~CMemDC(void) is defined in MemDC.h, and WaveFormDialog.cpp has
#include "MemDC.h"
tantormediaAsked:
Who is Participating?
 
HooKooDooKuConnect With a Mentor Commented:
It sounds like you are trying to define your own version of CMemDC when MFC already has one defined.  MFC didn't originally define a CMemDC class.  So if you recently upgraded MFC or installed a service pack, I could see where a new conflict might occur without any changes to your source code.

If you don't have your own CMemDC class defined, then you shouldn't need #include "MemDC.h" because it should already be included when you included "stdafx.h".

If you do have your own CMemDC class, then you need to either rename it or place it in your own namespace to resolve the conflict.

The quickest way to fix it with your own name space is to enclose the declaration of your CMemDC class (.h part) and the definition of CMemDC (.cpp part) inside a namespace...

#include "stdafx.h"
namespace MySpace{
class CMemDC::CDC
{
...
};
} //end of MySpace... no closing ;

//Do the same thing for the .cpp source code of your CMemDC.

 Then anywhere you are using your CMemDC, replace CMemDC with MySpace::CMemDC.  There is the 'using' key word that I think allows you to avoid having specify the name space with every instance of CMemDC, but I have not researched the 'using' keyword enough to advize you at this time.
0
 
tantormediaAuthor Commented:
Thank you very much. Yes, this problem appeared after an upgrade today. I just renamed MemDC of my own, and the trouble is gone.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.