1

I opened and upgraded a VC++6.0 project to VC2015. It compiles to this latest error, I've read through the posts pertaining this topic, didn't find a working solution. This involves the following class: MemDC

#if !defined(AFX_MEMDC_H__CA1D3541_7235_11D1_ABBA_00A0243D1382__INCLUDED_)
#define AFX_MEMDC_H__CA1D3541_7235_11D1_ABBA_00A0243D1382__INCLUDED_
#pragma once
// MemDC.h : header file
class CMemDC : public CDC
{
public:
    CMemDC(CDC* pDC);
    ~CMemDC();
    CMemDC* operator->() {return this;}
    operator CMemDC*() {return this;}

private:
    CBitmap  m_bitmap;
    CBitmap* m_pOldBitmap;
    CDC*     m_pDC;
    CRect    m_rect;
    BOOL     m_bMemDC;
};
#endif


// MemDC.cpp : implementation of MemDC class
// This class implements a memory Device Context
#include "stdafx.h"
#include "MemDC.h"

CMemDC::CMemDC(CDC* pDC) : CDC()
{
    ASSERT(pDC != NULL);

    m_pDC = pDC;
    m_pOldBitmap = NULL;
#ifndef WCE_NO_PRINTING
    m_bMemDC = !pDC->IsPrinting();
#else
    m_bMemDC = FALSE;
#endif

    if (m_bMemDC) // Create a Memory DC
    {
        pDC->GetClipBox(&m_rect);
        CreateCompatibleDC(pDC);
        m_bitmap.CreateCompatibleBitmap(pDC, m_rect.Width(), m_rect.Height());
        m_pOldBitmap = SelectObject(&m_bitmap);
#ifndef _WIN32_WCE
        SetWindowOrg(m_rect.left, m_rect.top);
#endif
    }
    else // Make a copy of the relevent parts of the current DC for printing
    {
#ifndef WCE_NO_PRINTING
        m_bPrinting = pDC->m_bPrinting;
#endif
        m_hDC = pDC->m_hDC;
        m_hAttribDC = pDC->m_hAttribDC;
    }
}

// Destructor copies the contents of the mem DC to the original DC
CMemDC::~CMemDC()
{
    if (m_bMemDC)
    {
        // Copy the offscreen bitmap onto the screen.
        m_pDC->BitBlt(m_rect.left, m_rect.top, m_rect.Width(), m_rect.Height(),
            this, m_rect.left, m_rect.top, SRCCOPY);

        //Swap back the original bitmap.
        SelectObject(m_pOldBitmap);
    }
    else {
        // All we need to do is replace the DC with an illegal value,
        // this keeps us from accidently deleting the handles associated with
        // the CDC that was passed to the constructor.
        m_hDC = m_hAttribDC = NULL;
    }
}

This class is not referenced by any other classes in the project, but when compiling it give the following errors:

LNK2005 "public: virtual __thiscall CMemDC::~CMemDC(void)" (??1CMemDC@@UAE@XZ) already defined in MemDC.obj  myProj C:\dev\myproj\nafxcwd.lib(afxglobals.obj)

I couldn't find the nafxcwd.lib in my project folder either.

I don't really see where the problem is.

For Comment
  • 1,139
  • 4
  • 13
  • 25
  • 1
    Sidenote: The lack of headers included by MemDC.h is going to be a real bummer someday. Every file should contain declarations for all of its requirements to prevent screw-ups when you move stuff around. – user4581301 Feb 20 '19 at 18:38
  • @user4581301 I've removed the headers to make this more concise, I get what you are saying that the headers might have some instructions per this issue, I checked, there isn't anything relevant to this issue. – For Comment Feb 20 '19 at 18:42
  • 1
    Groovy. Looks a lot like MFC. If you are building MFC on your own, odds are it's colliding with the built-in MFC. – user4581301 Feb 20 '19 at 18:57
  • 1
    Yes, pretty sure CMemDC is already a class in MFC. – john Feb 20 '19 at 19:09
  • So I suppose I can get rid this class from the project all together. – For Comment Feb 20 '19 at 19:11
  • 1
    ok, I renamed that class to be different from CMemDC, now it is working. So, I felt you two could share the credit. I am not sure how would that work. @john's definitive statement did prompt me to look into the MFC CMemDC class and found it. So, please post your response in answer so I can accept. – For Comment Feb 20 '19 at 19:41
  • Sort-of duplicate: https://stackoverflow.com/questions/5058852/lnk-2005-in-visual-c-in-visual-studio-2010 . Not 100% sure I want to hammer this shut, though. Might be a better-suited answer out there. – user4581301 Feb 20 '19 at 20:38
  • 1
    @user4581301, wow, that's exactly the same issue as this one, but I changed my own CMemDC class name to 'CMemDCm' and it works now. I bet VS2015 is better configured to handle this than VS2010 in that thread. Anyway, it's a fix for sure in this case. – For Comment Feb 21 '19 at 03:39

0 Answers0