Export (0) Print
Expand All
Expand Minimize

Compiler Errors when Implementing a CObject-Derived Class (Windows CE 5.0)

Windows CE 5.0
Send Feedback

When you implement a class derived from CObject and your code is written so that the copy constructor or assignment operator for the class needs to be called, the compiler may report errors similar to the following:

error C2660: 'CSample::CSample' : function does not take 1 parameters

error C2582: 'CSample' : 'operator =' function is unavailable

You can reproduce the problem by compiling the example in the Sample Code section below.

Note   The sample code shown in this article generates the following error messages:
error C2558: 'CSample::CSample' : no copy constructor available
error C2582: 'CSample' : 'operator =' function is unavailable

The reason for the compiler errors is that CObject declares a private copy constructor and assignment operator in the AFX.h file. Consequently, the compiler does not generate a default copy constructor and assignment operator for the CObject-derived class.

Because the compiler does not find these functions declared in the class, it reports the errors.

To avoid the compiler errors, you need to implement a copy constructor and assignment operator for the CObject-derived class. This is illustrated in the sample code below.

The following example shows how you can avoid errors by removing the comments from the lines indicated.

/* Compile options needed: /c
*/
 
// replace with #define _CONSOLE when compiling for Windows NT
#define _DOS
 
#include <afx.h>
 
class CSample : public CObject
{
private:
    short m_nValue;
public:
// uncomment the lines below to avoid the compiler errors
//    CSample() {}
//    CSample( const CSample &s )  // copy ctor
//        { m_nValue = s.m_nValue; }
//    CSample& operator=( const CSample &s )  // assignment operator
//        { m_nValue = s.m_nValue; return *this; }
};
 
void main()
{
    CSample a;
    CSample b = a;
    a = b;
}


Send Feedback on this topic to the authors

Feedback FAQs

© 2006 Microsoft Corporation. All rights reserved.


Show:
© 2014 Microsoft