Solution to VC++ compiler error reporting error C2248
- 2020-05-05 11:38:58
- OfStack
This error often occurs when using classes such as :CArray or CList
The cause of this error is due to the custom class array item when
There is an operation such as Add() in this operation, actually needs a = operation, but this = operation is not implemented in the custom class, so the program automatically goes to its parent class CObject class to find, but finds that the = of this class is an private and reports this error.
Once you know the cause, the solution is to override the operator = in the custom class and the error disappears.
class COptRect : public CObject
{
public:
COptRect();
virtual ~COptRect();
// The start range of the operation
CRect m_OptStartRect;
// The end range of the operation
CRect m_OptEndRect;
// The target interface of the operation
int m_OptDesSurface;
COptRect& operator = (COptRect & src);
};
Implementation code
COptRect::COptRect()
: m_OptDesSurface(0)
{
}
COptRect::~COptRect()
{
}
COptRect& COptRect::operator = (COptRect & src)
{
this->m_OptDesSurface = src.m_OptDesSurface;
this->m_OptEndRect = src.m_OptEndRect;
this->m_OptStartRect = src.m_OptStartRect;
return *this;
}
So after implementing the custom class, start using
Let's define the array of variables,
CArray<COptRect, COptRect&> optArray;
After this array, we use a command
to add new elements
// Add an action area to the interface
void CSurface::AddOptRect(CRect Start, CRect End, int DesID)
{
COptRect ort;
ort.m_OptStartRect = Start;
ort.m_OptEndRect = End;
ort.m_OptDesSurface = DesID;
optArray.Add(ort);
}
After such operation, no more newspaper error! Problem solving
Case 2:
error C2248: "CObject::operator =" : unable to access the private member (declared in the "CObject" class), unable to locate that part of the code problem,
Many people have also encountered this problem on the Internet.
The analysis of the program I wrote is different from the previous, one of them is a place to use Image Picture control CStatic class object, the first pointer object, later changed to an object in addition to the above problems, changed back to ok.
Later, I checked that it was best to declare Pointers to objects that operated on the control, or else you would get a bug.