- Why the compiler gives me an error when I don’t initialize a value to a static member variable? Shouldn’t it be initialized to 0?
- Why I have to initialize member variable outside the class? (is this illegal because if you do so, and change the value of this static member variable inside the main function and you create an object of this clas, it will re-assign the static mamber variable to the old value) whereas const static member variable are legal to be initilized inside the class (and this is possible because you can’t change the value of this static member variable anyway)?
Error: undefined reference to class_name::a
From the error posted, the linker is stating that the variable has not been defined, not that it has not been explicitly initialised:
The linker should not emit an error and the compiler should not emit a warning, as long as no attempt is made to use the
staticvariable before it has been assigned an initial value.