Developing Windows Applications by Mykola Dudar

Helping Developers Build Applications for Windows using Visual Studio.

cannot access private member error when porting MFC project

cannot access private member error when porting MFC project

  • Comments 5

On the forum, we have surfaced another error. I had a chance today to see a real-world code with this error and actually to narrow down the problem. So if you see an error like

1>c:\program files\microsoft visual studio 8\vc\atlmfc\include\afxwin.h(259) : error C2248: 'CObject::operator =' : cannot access private member declared in class 'CObject'

1> c:\program files\microsoft visual studio 8\vc\atlmfc\include\afx.h(540) : see declaration of 'CObject::operator ='

1> c:\program files\microsoft visual studio 8\vc\atlmfc\include\afx.h(510) : see declaration of 'CObject'

1> This diagnostic occurred in the compiler generated function 'CGdiObject &CGdiObject::operator =(const CGdiObject &)'

The cause of this error is that you are have a class like

class CReproClass {

public:

CReproClass::CReproClass(void) {}

CReproClass::~CReproClass(void) {}

CPen m_pen; //or any other class that subclasses CGdiObject

};

or

class CReproClass : public CPen{

...
};


then is later used in 

CReproClass a;
CReproClass b;
a = b;

This is the cause of the problem.

  • Thanks for the help, this saved me hours!

  • A hundred thousand line code project + a weeks worth of work = tracking down bugs a pain in the ass. Thanks for pointing out the problem. It saved me alot of work.

  • Hey dude, thank you so much for this!!!  I've been looking everywhere for a simple, yet comprehensive explanation for this compilation error, and nothing can be found.  

    This really hits the spot.  Thank you so much for this!!

  • Here is my problem similar ti this.

    i have written a code to update my combo box content to my local structure.Something like following.At this point the error is comming.

    void CMeterpollingConfiguration::getMeterPoolingConfigData()

    {

    #if 1

       COleDateTime DateTime;

       sprintf(g_stMeterpollingconf.cDateTimeBC,"%04d-%02d-%02d %02d:%02d:%02d", DateTime.GetYear(), DateTime.GetMonth(), DateTime.GetDay(), DateTime.GetHour(), DateTime.GetMinute(), DateTime.GetSecond());

    #endif

       if(m_MeterpollingBillingData == TRUE)

       {

           g_stMPCTypeofdata.cbMPCBMinute = m_MpcBdMinutes;

           g_stMPCTypeofdata.csMPCBSubminutes = m_MpcBdMinute;

           g_stMPCTypeofdata.cbMPCBHours = m_MpcBdHours;

           g_stMPCTypeofdata.cbMPCBDay = m_MpcBdDay;

           g_stMPCTypeofdata.cbMPCBDate = m_MpcBdDate;

       }

       else if(m_MpcLoadServyData == TRUE)

       {  

           g_stMPCTypeofdata.cbMPCBMinute = m_MpcLsdMinutes;

           g_stMPCTypeofdata.csMPCBSubminutes = m_MpcLsdMinute;//m_MpcLsdMinute

           g_stMPCTypeofdata.cbMPCBHours = m_MpcLsdHours;

           g_stMPCTypeofdata.cbMPCBDay = m_MpcLsdDay;

           g_stMPCTypeofdata.cbMPCBDate = m_MpcLsdDate;

       }

       else if(m_MPCFullData == TRUE)

       {  

           g_stMPCTypeofdata.cbMPCBMinute = m_MpcFdMinutes;

           g_stMPCTypeofdata.csMPCBSubminutes = m_MpcFdminute;//m_MpcLsdMinute

           g_stMPCTypeofdata.cbMPCBHours = m_MpcFdHours;

           g_stMPCTypeofdata.cbMPCBDay = m_MpcFdDay;

           g_stMPCTypeofdata.cbMPCBDate = m_MpcFdDate;

       }

       else if(m_MpcTamperData == TRUE)

       {  

           g_stMPCTypeofdata.cbMPCBMinute = m_MpcTdMinutes;

           g_stMPCTypeofdata.csMPCBSubminutes = m_MpcTdMinute;//m_MpcLsdMinute

           g_stMPCTypeofdata.cbMPCBHours = m_MpcTdHours;

           g_stMPCTypeofdata.cbMPCBDay = m_MpcTdDay;

           g_stMPCTypeofdata.cbMPCBDate = m_MpcTdDate;

       }

       else if(m_MpcInstData == TRUE)

       {  

           g_stMPCTypeofdata.cbMPCBMinute = m_MpcIdMinutes;

           g_stMPCTypeofdata.csMPCBSubminutes = m_MpcIdMinute;//m_MpcLsdMinute

           g_stMPCTypeofdata.cbMPCBHours = m_MpcIdHours;

           g_stMPCTypeofdata.cbMPCBDay = m_MpcIdDay;

           g_stMPCTypeofdata.cbMPCBDate = m_MpcIdDate;

       }

       else

       {}

    return;

    }

    here pls note that :i'm updating different combo box content to same structure variables,by using if...else if ..else statement.

    while compilation i 'm getting error " 'CObject::operator =' : cannot access private member declared in class 'CObject'" which is in compiler genetated file(afxwin.h).

    Pls help me to slove this problem.

    Thanks.

  • I'm confused there.

    So, which line of code exactly is causing the error? And how to correct?

    I have many CObject and CPen etc. If I have to correct each line, it's going to be a nightmare.

    Please respond or email me.

    Thanks,

    Super

Page 1 of 1 (5 items)