static int a;
This declares the static variable. That tells the compiler that this field exists and would be of type int
. This is useful information for compiling the code.
But the variable still needs to be defined. That is some translation unit(and exactly one translation unit) needs to allocate the memory for this variable. See What is a “translation unit” in C++
For other NITs of declaration and definition check this question: What is the difference between a definition and a declaration?
As to the solution you need to add a definition as well outside the class:
int Demo_StaticVar::a;
int main(){
...
Code link: https://ideone.com/l7ie7p
3
solved ERROR : undefined reference to classname::member_variable