问题描述
我在 MFC (Visual Studio 2013) 中为 WinCE 2013 开发了一个简单的程序,使用 GDI 方法在设备上下文上绘图.不幸的是,当我尝试在上下文设备句柄上调用 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;
}
我尝试用谷歌搜索这个错误,但我找不到可以帮助我的东西.
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"类中声明的私有成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!