VC ++ error C2248: & ldquo; CObject: CObject & rdquo;: unable to access private members (declared in the & ldquo; CObject & rdquo; Class), c2248cobject

Source: Internet
Author: User

VC ++ error C2248: "CObject: CObject": private Members cannot be accessed (declared in the "CObject" class), c2248cobject
This error often occurs when you use classes such as CArray or CList.

This error is caused

There is an operation such as Add (). In this operation, a = operation is actually required, but this = operation is not implemented in the Custom class. Therefore, the program automatically goes to its parent class, that is, the CObject class, but finds the = of this class is a private, so this error is reported.

After knowing the cause, the solution will naturally be available, that is, the error will disappear after the overload operator = overload in the Custom class.

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;}

 

Then, after implementing this custom class, start to use it.

First define the variable array

CArray<COptRect, COptRect&> optArray;

After this array, we use a command to add new elements.

// Add an operation region 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 this operation, the following error will not be reported! Problem Solving

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.