0

This problem has made me feel blue, and I can't work out how to fix it.

class A{
public:
    static int get_A() {
        return a;
    }
    static int get_B() {
        return b;
    }
    static int a;
    static int b;
};

int main() {
    int c = A::get_A(); 
    return 0;
}

I wanna use static an in static method get_A(), but I got compiling error as below. (please forgive me using the meanless variable name in the program like above)

Undefined symbols for architecture x86_64:
  "A::a", referenced from:
      A::get_A() in main.o
冯剑龙
  • 569
  • 8
  • 22
  • Here's a dupe more relevant to your question: [Undefined reference to static variable](https://stackoverflow.com/questions/14331469/undefined-reference-to-static-variable) – TrebledJ Apr 26 '19 at 09:51
  • 你需要在类外面定义静态变量:int A::a = 0; int A::b = 0; – travaller2 Apr 26 '19 at 10:26

0 Answers0