• Register

C++ has few boundations and they all had their reasons for why they exist. “ISO C++ forbids in class initialization of non const static members” is one of them. In this article we will discuss its reasons and solutions.

Cause of the error

The standard C++ does not allow this, it only allows static constant integral or enumeration type to be initialized inside the class.

The reason C++ gives is “A class is typically declared in a header file and a header file is typically included into many translation units. However, to avoid complicated linkers rules, C++ requires that every object has a unique definition. That rule would be broken if C++ allowed in-class definition of entities that needed to be stored in memory as objects.”

If you don’t know about static data members and fundamental types then you should learn about them first.


using namespace std;

class ts{
  static int counter=0;
  void printC(){


int main(){
  ts a;
  ts b;

  return 0;



The easiest solution for the code given should be:

We have to declare a counter in the class body like this. 

static int counter;

And define it outside the class like this.

int ts::counter = 0;

Hope this helps.

100 points
7 2