MFC: linking error

Posted on 2011-05-04
Last Modified: 2012-05-11
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"
Question by:tantormedia
    LVL 16

    Accepted Solution

    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.

    Author Closing Comment

    Thank you very much. Yes, this problem appeared after an upgrade today. I just renamed MemDC of my own, and the trouble is gone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
    Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now