Explanation
You have only declared your static data-member named A::Aptr
, but in order to use it, as you are inside the constructor of A
, you must provide a definition.
The linker diagnostic might say undefined reference, which might be hard to relate to a missing definition, but it's talking about its internal mapping of names and storage location.
In other words; the linker is unable to find a a reference (ie. entry) for the location where A::Aptr
is stored, which makes sense: Without a definition you haven't given A::APtr
any storage to use.
Solution
You've already provided both a declaration (2), and a definition (4) for A::A_count
, if you do the same for A::APtr
you will be all set.
class A {
private:
static A* Aptr[5]; // (1), declaration
public:
static int A_count; // (2), declaration
A() {
Aptr[A_count] = this;
}
};
A* A::APtr[5]; // (3), definition <-- previously missing!
int A::A_count = 0; // (4), definition
Note: You are only asking about the linker error, but I'm guessing you mean to increment A_count
upon constructing an object of type A
, something which you are currently not doing.