Resolving error C2248 with CObject

22 December, 2008 § 1 Comment

Ever come across this error before?

error C2248: 'CObject::CObject' : cannot access private member declared in class 'CObject'

It can be a frustrating error to try and diagnose. Try commenting out all the code you’ve added since your last checkin and the error is still there? Most likely, this is happening due to a copy constructor being called on an object that derives from CObject. If you are returning a CObject by value, then the copy constructor will be implicitly called. To solve this, either return it by reference or by const-reference, which ever applies to the situation.

This means if you have this:

CProgressCtrl CMyWindow::GetProgressDialog() { ... }

You should change it to this:

void CMyWindow::GetProgressDialog( CProgressCtrl& progressCtrl ) { ... }

If you have this:

void CMyWindow::UpdateProgressDialog( int position, CProgressCtrl progressCtrl ) { ... }

You should change it to this:

void CMyWindow::UpdateProgressDialog( int position, const CProgressCtrl* progressCtrl ) { ... }

Thank you goes out to Nick Meyer. Sometimes the obvious reasons just get left out when trying to figure out why something doesn’t work.

Where Am I?

You are currently browsing entries tagged with cobject at JAWS.