简体   繁体   中英

error C2248: 'CObject::CObject' : cannot access private member declared in class 'CObject' when I calling hDC.SelectObject function in MFC

I develop a simple program in MFC (Visual Studio 2013) for WinCE 2013, using GDI methods for drawing on device context. Unfortunatelly when I try to call SelectObject on context device handle i get error: "error C2248: 'CObject::CObject': cannot access private member declared in class 'CObject"

I attach one of functions which calls SelectObject method.

    BOOL Druk::DrawGrid(CDC hDC,int start_x, int start_y, int limit_x, int limit_y, int width)
{
    CPen pen;
    COLORREF linecol;
    pen.CreatePen(PS_SOLID, width, NULL);
    hDC.SelectObject(&pen);
    for (float i = start_y; i < limit_y; i += 5 * MILIMETER)
    {
        hDC.MoveTo(start_x, i);
        hDC.LineTo(limit_x, i);
    }
    for (float j = start_x; j < limit_x; j += 5 * MILIMETER)
    {
        hDC.MoveTo(j, start_y);
        hDC.LineTo(j, limit_y);

    }
    for (float i = start_x; i < limit_x; i += MILIMETER)
    {
        for (float j = start_y; j < limit_y; j += MILIMETER)
        {
            hDC.MoveTo(i, j);
            hDC.LineTo(i + 1, j);
        }
    }

    return TRUE;
}

I try to google this error but I cannot find sth what can help me.

Your code for SelectObject() looks fine to me. HOWEVER, passing a CDC by value is a big error. You should pass it by reference or pass a pointer to a CDC. I would expect to maybe see an error for when the argument CDC hDC tries to make a copy. The copy constructor and assignment operator for CObject are declared private and unimplemented. You cannot make a copy of them. Instead, change the signature of your function to:

BOOL Druk::DrawGrid(CDC& hDC,int start_x, int start_y, int limit_x, int limit_y, int width)
{
// your code
}

You also have some other problems... you need to save the originally selected pen and then select it back into the CDC at the end....

CPen* pOldPen = hdc.SelectObject(&pen);

at the end

hdc.SelectObject(pOldPen);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM