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.
Another useful technique is to insert
#pragma message (“checkpoint keyword\n”)
anywhere in your code and then look at the output window of the compilation.
Once after a few Ctrl-F7 compiles you should be able to find the exact class header file of yours that the compiler does not like.
After that placing pragma messages inside and outside of the class will get you to your “line of code” that is causing CC2248.
In my case vc6 and express visual c++ 2010 (with sdk7 something) compiled my code but visual studio 2012 pro, shrink wrapped is not happy.