Compiler Error C2864
TOC
Collapse the table of content
Expand the table of content
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer

Compiler Error C2864

'variable' : only static const integral data members can be initialized within a class

You attempted to initialize a data member that was not defined as const and static.

The following sample generates C2864:

// C2864.cpp
class B  {
   int i = 3;   // C2864; 
   static const int i = 3;   // OK
};

The following sample generates C2864:

// C2864b.cpp
// compile with: /c
#include <stdio.h>
class BaseCls {
public:
   int Z;
};

class DervCls : public BaseCls {
public:
   char Z;
};

class UseClasses {
public:
   BaseCls * BObj = new DervCls;   // C2864
   DervCls * DObj = new DervCls;   // C2864 
   // Uncomment the following 2 lines to resolve.
   // BaseCls * BObj;// = new DervCls;
   // DervCls * DObj;// = new DervCls;

   void ShowZ() {
      // Uncomment the following 2 lines to resolve.
      // BObj = new DervCls;
      // DObj = new DervCls;
      printf("Accessed via base class: %d", BObj->Z);
      printf("Accessed via derived class: %c", DObj->Z);
   }
};
Show:
© 2016 Microsoft