错误C2248:"CObject :: CObject":当我在MFC中调用hDC.SelectObject函数时,无法访问在类"CObject"中声明的私有成员 [英] error C2248: 'CObject::CObject' : cannot access private member declared in class 'CObject' when I calling hDC.SelectObject function in MFC
问题描述
我使用GDI方法在设备上下文上进行绘制,在WinCE 2013的MFC(Visual Studio 2013)中开发了一个简单的程序.不幸的是,当我尝试在上下文设备句柄上调用SelectObject时,出现错误:错误C2248:'CObject :: CObject':无法访问在类'CObject'中声明的私有成员"
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"
我附上了一个调用SelectObject方法的函数.
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;
}
我尝试通过Google搜索此错误,但找不到任何可以帮助我的东西.
I try to google this error but I cannot find sth what can help me.
推荐答案
您对SelectObject()的代码对我来说很好.但是,按值传递CDC是一个很大的错误.您应该通过引用传递它或传递指向CDC的指针.当参数CDC hDC尝试进行复制时,我可能希望看到错误.CObject的复制构造函数和赋值运算符被声明为私有且未实现.您无法复制它们.而是将函数的签名更改为:
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
}
您还遇到其他一些问题...您需要保存最初选择的笔,然后最后将其重新选择到CDC中....
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);
最后
hdc.SelectObject(pOldPen);
这篇关于错误C2248:"CObject :: CObject":当我在MFC中调用hDC.SelectObject函数时,无法访问在类"CObject"中声明的私有成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!