Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2005
Visual Studio
Visual C++
C/C++ Build Errors
 Compiler Warning (level 4) C4512
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
Visual C++ Concepts: Building a C/C++ Program
Compiler Warning (level 4) C4512

Error Message

'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.

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 to suppress the warning.

Example

The following sample generates C4512.

// C4512.cpp
// compile with: /EHsc /W4
// processor: x86

class base {
private:
   const int a;

public:
   base(int val = 0) : a(val) {}
   int get_a() { return a; }
};   // C4512 warning

class base2 {
private:
   const int a;

public:
   base2(int val = 0) : a(val) {}
   base2 & operator=( const base2 & ) {}
   int get_a() { return a; }
};

int main() {
   base first;
   base second;

   // OK
   base2 first2;
   base2 second2;
}
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2010 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker