Q:

Accesing staic variable from static function C++

class A {
public:
    static int i;
    static int  inc();
};

int A::inc() {
    i++;
    return i;
}

int main() {
    A::inc();
}

As we know that we can access static member variables from static method.
But this code is giving me a error
"A::i", referenced from:
  A::inc()     in ccn8PKhC.o
ld: symbol(s) not found for architecture x86_64

Can some one please tell me what am i missing

A:

This is a linker error. You've declared the variable, but you haven't defined it, so the linker can't find it.
Add this:
int A::i = 0;

somewhere globally in a .cpp file (outside your class and any function) to define it. If you have multiple .cpp files in your project, the definition needs to be in only one of them.

