I have a class A
which has a static instance member A* a
.
I am trying to create a static method initA()
which initiates a
.
However, if I implement the method in a cpp
file instead of the header file, I get an undefined reference error:
The code for the header file:
class A
{
public:
static A *a;
static void initA ();
};
The code for the cpp
file:
#include "A.h"
void A::initA(){
A::a = new A();
}
Compiling this code yields the following error (copied from an online IDE. My VS yields different error but its meaning is the same):
/var/tmp/ccW1EJl7.o: In function `A::initA()':
A.cpp:(.text+0x11): undefined reference to `A::a'
collect2: error: ld returned 1 exit status
Note that replacing the line A::a = new A();
with a = new A();
(omitting the A::
) yields the same error.
Any ideas what am I doing wrong and how can I implement the method correctly?