-2

i've got a problem dealing with static private variables. Here is my code.

ClassA.h:

class ClassA{
    static int a;
public:
    int getA();
};

Class.cpp:

#include "ClassA.h"

int ClassA::a = 9001;

int ClassA::getA(){
    return a; //<--- Undefined reference to ClassA::a
}

As you can see, i defined the static variable in the implementation file, as it has been said over and over, i also did this to other static member of other classes, and they work without any problem, but this one doesn't. Does anyone have any solutions?

TimeZero
  • 1
  • 1

2 Answers2

0

Maybe ClassA is missing a ; at the end of its declaration?

I can't spot any errors in the code you've provided. Perhaps ClassA is defined inside a namespace?

S.C. Madsen
  • 5,100
  • 5
  • 32
  • 50
-2

declare

int ClassA::a; in .h itself

as below:

class ClassA{
    static int a;
public:
    int getA();
};
int ClassA::a = 9001;

Also, Your class A declaration does not end with a semicolon.

Dr. Debasish Jana
  • 6,980
  • 4
  • 30
  • 69