且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

错误 C2248:“CObject::CObject":当我在 MFC 中调用 hDC.SelectObject 函数时,无法访问在“CObject"类中声明的私有成员

更新时间:2023-02-14 23:29:10

您的 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);