This documentation is archived and is not being maintained.

Compiler Warning (level 4) C4512

'class' : assignment operator could not be generated

The compiler cannot generate an assignment operator for the given class. No assignment operator was created.

An assignment operator for the base class that is not accessible by the derived class can cause this warning.

To avoid this warning, specify a user-defined assignment operator for the class.

The compiler will also generate an assignment operator function for a class that does not define one. This assignment operator is simply a member wise copy of the data members of an object. Because const data items cannot be modified after initialization, if the class contains a const item, the default assignment operator would not work.

For example, the following code generates C4512:

// C4512.cpp
// compile with: /EHsc /W4
#include <iostream>
using namespace std;
class base
   const int a;
   base(int val = 0) : a(val)
   int get_a(void)
   return a;
};   // C4512

int main()
   base first;
   base second(5);

   cout << "First = " << first.get_a() << endl;
   cout << "Second = " << second.get_a() << endl;

You can resolve the C4512 warning for this code in one of three ways:

  • Explicitly define an assignment operator for the class.
  • Remove const from the data item in the class.
  • Use the #pragma warning statement.