Microsoft KB Archive/87638

= You receive a &quot;C4512&quot; error message when Microsoft C/C++ version 7.0 or later is used to compile a C++ source file that uses a class containing a const data member =

Article ID: 87638

Article Last Modified on 1/9/2006

-

APPLIES TO


 * Microsoft Visual C++ 1.0 Professional Edition
 * Microsoft Visual C++ 1.5 Professional Edition
 * Microsoft Visual C++ 1.51
 * Microsoft Visual C++ 1.52 Professional Edition
 * Microsoft Visual C++ 1.0 Professional Edition
 * Microsoft Visual C++ 2.0 Professional Edition
 * Microsoft Visual C++ 2.1
 * Microsoft Visual C++ 4.0 Standard Edition
 * Microsoft Visual C++ 4.1 Subscription
 * Microsoft Visual C++ 4.2 Professional Edition
 * Microsoft Visual C++ 5.0 Standard Edition
 * Microsoft Visual C++ 6.0 Service Pack 5
 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft Visual C++ .NET 2002 Standard Edition

-



This article was previously published under Q87638



SYMPTOMS
The following warning may be generated when Microsoft C/C++ version 7.0 or later is used to compile a C++ source file that uses a class containing a const data member:

C4512:  : assignment operator could not be generated

where class_name is the name of the class containing the const data member.



CAUSE
The compiler will generate an assignment operator function for a class that does not define one. This assignment operator is simply a memberwise 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.



RESOLUTION
The C4512 warning will not prevent the generation of an object module. The warning can be eliminated by any of the following methods:  Explicitly define an assignment operator for the class.

-or-

 Remove the const data item from the class.

-or-

 Use the #pragma warning statement.



MORE INFORMATION
The following sample C++ code demonstrates this situation.

Sample Code
/* Compile options needed: /W4

// In Visual C++ 2005, you shoul add this &quot;#include &quot;stdafx.h&quot;&quot; in. class base {  const int a;
 * 1) include 

public: base(int val = 0) : a(val) {} int get_a(void) { return a; } };

void main {  base first; base second(5);

cout << &quot;First = &quot; << first.get_a << '\n';

cout << &quot;Second = &quot; << second.get_a << '\n'; }

Additional query words: 8.00 8.00c 9.00 9.10

Keywords: kbprb kbcpponly kbcompiler KB87638

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.