There is a class:

class A { public: static int count; A() { count++; // При создании каждого объекта эта переменная должна увеличиваться } }; 

It is necessary to assign the value of this variable to 0, before calling the constructor. But if you do it like this:

 static int count = 0; 

That compiler swears. How to manage in this situation?

    1 answer 1

    You just need to correctly define this variable outside the class.

     class A { public: static int count; A() { count++; // При создании каждого объекта эта переменная должна увеличиваться } }; //... int A::count = 0; 

    You can also omit the initializer, since the variable will in any case be initialized to zero:

     int A::count; 

    If the class definition is placed in the header file, then the static variable definition must be placed in one of the program modules.