PRB: Compiler Errors when Implementing CObject-Derived Class (117794)
The information in this article applies to:
- The Microsoft Foundation Classes (MFC), when used with:
- Microsoft C/C++ for MS-DOS 7.0
- Microsoft Visual C++ 1.51
- Microsoft Visual C++ 1.52
- Microsoft Visual C++, 32-bit Learning Edition 4.0
- Microsoft Visual C++, 32-bit Professional Edition 2.0
- Microsoft Visual C++, 32-bit Professional Edition 4.0
This article was previously published under Q117794 SYMPTOMS
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 sample code in the
"RESOLUTION" section, below.
NOTE: Using Visual C++, 32-bit Edition, versions 2.x and 4.0, 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
CAUSE
The reason for the compiler errors is that CObject declares a private copy
constructor and assignment operator in the AFX.H file. The compiler does
not generate a default copy constructor and assignment operator for the
CObject-derived class because of this. Because the compiler does not find
these functions declared in the class, it reports the errors.
RESOLUTION
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. You can avoid the errors by uncommenting the lines
indicated in the sample code.
Sample Code/* 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;
}
REFERENCES
"Visual C++ and C/C++, version 7.0 Language Reference" manuals,
Section 11.7, "Copying Class Objects."
Visual C++ 4.0 Books Online: Visual C++ Books, C/C++, C++ Language
Reference, Special Memeber Functions, Copying Class Objects.
Modification Type: | Major | Last Reviewed: | 12/1/2003 |
---|
Keywords: | kbLangCPP kbprb KB117794 |
---|
|