0

Making my Breakout Game in MFC. I store the bitmap info into a CDC object (Device Context Object), but since there will be different bitmaps for each block, I store the same overwritten CDC m_blockDC into a std::vector<CDC> m_blockStates and since I have five block types I store the before mentioned vector into a std::vector<std::vector<CDC>> so I can access each block state bitmap info using a enum BLOCKPROPS and their hit property which is vinculated with the index; thus, I will be accessing each block state with m_blockStates[BLOCKTYPE][CBlock.GetHits()]

When I try to run the program the error 'CDC::CDC(const CDC &)': attempting to reference a deleted function appears.

This is how I store the m_blockDC into the bidimensional vector:

void CCreateWindow::LoadBlock(std::vector<LPCWSTR> FileMap, BLOCKPROP BlockProperty) 
{
    for (int i = 0; i < FileMap.size(); i++)
    {
        HANDLE blockBitmap = LoadImage(0, FileMap[i], IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE); //(Instance, FileSrc, Type, PartX, PartY, Process)
        m_blockCBMP.Attach(reinterpret_cast<HBITMAP>(blockBitmap));
        pDC = this->GetDC();
        m_blockDC.CreateCompatibleDC(pDC);

        switch (BlockProperty)
        {
        case TUFF:
            m_blockStateArray[TUFF].push_back(m_blockDC);
            break;
        case STRD:
            m_blockStateArray[STRD].push_back(m_blockDC);
            break;
        case WEAK:
            m_blockStateArray[WEAK].push_back(m_blockDC);
            break;
        case SPCL:
            m_blockStateArray[SPCL].push_back(m_blockDC);
            break;
        case INDS:
            m_blockStateArray[INDS].push_back(m_blockDC);
            break;
        default:
            break;
        }
    }

    //Get BitmapSize
    m_blockDC.SelectObject(&m_blockCBMP);
    m_blockCBMP.GetBitmap(&m_blockBMP);
}

This code is used in the OnInitDialog() of CCreateWindow.cpp and the function PaintBlock() within OnTimer() will be used to paint the blocks, or at least that's how it is suppossed to be.

zett42
  • 25,437
  • 3
  • 35
  • 72
  • Looks like `CDC` is not copyable. Can you provide a [mcve]? – NathanOliver Aug 09 '18 at 19:22
  • 3
    `std::vector` requires elements that can be copied and/or moved, but `CDC`'s copy constructor is `delete`d, so `CDC` objects can't be copied. That is what the compiler is complaining about. Try using `std::unique_ptr` elements in your vector instead, as `std::unique_ptr` is compatible with `std::vector`. – Remy Lebeau Aug 09 '18 at 19:24
  • 4
    The `CDC` is not copyable. What exactly your code does with it doesn't matter. A class may not be copyable for any number of reasons. `CDC`, according to Microsoft's documentation, represent a device context. The most likely reason is that the notion of "copying a device context" is meaningless, and it basically cannot be copied for the same reason that `std::istream` cannot be copied, basically. – Sam Varshavchik Aug 09 '18 at 19:25
  • You probably need to redesign your program. `GetDC` is a temporary handle, it has to be cleaned up with `ReleaseDC`. Paint in response to `ON_WM_PAINT`, and use `CPaintDC dc(this)` which has automatic cleanup. – Barmak Shemirani Aug 09 '18 at 22:05

0 Answers0