PRB: Compiler Errors when Implementing CObject-Derived Class

ID: Q117794

7.00 | 1.00 1.50 1.51 1.52 | 1.00 2.00 2.10 4.00

MS-DOS | WINDOWS             | WINDOWS NT
kbprg kbprb

The information in this article applies to:

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.

Additional reference words: 7.00 1.00 1.50 2.00 2.10 2.50 1.51 1.52 2.51 3.00 3.10 4.00 arguments KBCategory: kbprg kbprb KBSubcategory: MfcMisc

Keywords          : kbnokeyword kbMFC kbVC 
Version           : 7.00   | 1.00 1.50 1.51 1.52 | 1
Platform          : MS-DOS NT WINDOWS

Last Reviewed: August 1, 1997