locked
'CObject::operator =' : cannot access private member declared in class 'CObject' RRS feed

  • Question

  • Help, please, I can't find the reason of ERROR:

    Error 32 error C2248: 'CObject:Surpriseperator =' : cannot access private member declared in class 'CObject'  

     

    Code Snippet

    class CfcString : public CObject

    {

    //Constructors/Destructor

    public:

    CfcString();

    CfcString(LPWSTR lpwsData, int nLen);

    CfcString(LPSTR lpsData, int nLen);

    CfcString(CString& sUData);

    ~CfcString();

    //Properties

    private:

    int m_nLength; //Number of characters on the string

    LPBYTE m_pAnsiSBData; //ANSI string single Byte

    LPWSTR m_pAnsiDBData; //ANSI string double Byte

    //methods

    public:

    int GetLength() { return m_nLength; };

    LPBYTE GetAnsiSBString() { return m_pAnsiSBData; };

    LPWSTR GetAnsiDBString() { return m_pAnsiDBData; };

    static LPWSTR ConvertToUnicode( LPBYTE lpsData, int nInLen = -1, BOOL bArabic=FALSE);

    static LPBYTE ConvertFromUnicode( LPWSTR lpwsData, int nInLen = -1, BOOL bArabic=FALSE);

    static LPBYTE ConvertToSingleBYTEString( LPWSTR lpwsData, int nLen = -1);

    static LPWSTR ConvertToDoubleBYTEString( LPBYTE lpsData, int nLen = -1);

    #if defined(UNICODE)

    static void ConvertToDoubleBYTEString( CString& sData, LPBYTE lpsData,

    int nLen = -1);

    #endif //UNICODE

    //Implementation

    public:

    virtual void Serialize(CArchive& ar);

    private:

    void Init();

    void CleanUp();

    UINT ReadStringLength(CArchive& ar);

    static LPWSTR CreateBuffer(LPBYTE lpsData, int &nLen, BOOL bArabic=FALSE);

    static LPBYTE CreateBuffer(LPWSTR lpwsData, int &nLen, BOOL bArabic=FALSE);

     

    DECLARE_SERIAL(CfcString)

    };

     

     

    Monday, January 28, 2008 9:30 AM

Answers

  • CObject declares a private copy constructor and operator=.
    So, the compiler doesn't generate a default copy constructor and operator= for the CObject-derived class.

    You need to implement copy constructor and assignment operator for the CObject-derived class yourself.


    It is also documented here, with sample code:

    http://msdn2.microsoft.com/en-us/library/ccb3dh5c.aspx

     

    Giovanni

     

    Tuesday, January 29, 2008 1:55 PM

All replies

  • Maybe you should also declare the copy-constructor as well as several = operators? Can you find the line which causes this error? Note that some of features are probably already provided by existing CStringA and CStringW classes.

     

    Monday, January 28, 2008 9:53 AM
  • CObject declares a private copy constructor and operator=.
    So, the compiler doesn't generate a default copy constructor and operator= for the CObject-derived class.

    You need to implement copy constructor and assignment operator for the CObject-derived class yourself.


    It is also documented here, with sample code:

    http://msdn2.microsoft.com/en-us/library/ccb3dh5c.aspx

     

    Giovanni

     

    Tuesday, January 29, 2008 1:55 PM
  • Why oh why did Microsoft do that?? It's extremely irritating that MFC forces me to write custom copy constructors / assignment operators when the compiler-generated ones would do JUST FINE. :-((
    Thursday, October 29, 2009 4:19 PM
  • The problem comes about when you derive from CObject.  But if you do not need the features of CObject then you do not have to derive your class from it.
    Thursday, October 29, 2009 5:11 PM
  • The problem comes about when you derive from CObject.  But if you do not need the features of CObject then you do not have to derive your class from it.
    Hi Scott,

    The problem is, I do! I got a class that needs to be serializable and therefore must be derived from CObject, but the compiler-generated copy constructor and assignment operator would still be entirely sufficient, if it wasn't for the fact that CObject is non-copyable. Normally this shouldn't be a big deal, but the class in question has a whole bunch of member variables and it's easy to forget one when writing a custom copy constructor / assignment operator. Ah well, no way around it, I guess.
    Friday, October 30, 2009 8:16 PM