Undefined reference to member in custom class

I attempted to compile the following code using g++ and I got the following error:
$ g++ test.cpp

/tmp/ccjE1ooy.o: In function `A::A()':
test.cpp:(.text._ZN1AC2Ev[_ZN1AC5Ev]+0x16): undefined reference to `A::allA'
/tmp/ccjE1ooy.o: In function `A::~A()':
test.cpp:(.text._ZN1AD2Ev[_ZN1AD5Ev]+0x1d): undefined reference to `A::allA'
test.cpp:(.text._ZN1AD2Ev[_ZN1AD5Ev]+0x40): undefined reference to `A::allA'
test.cpp:(.text._ZN1AD2Ev[_ZN1AD5Ev]+0x6f): undefined reference to `A::allA'
test.cpp:(.text._ZN1AD2Ev[_ZN1AD5Ev]+0xa8): undefined reference to `A::allA'
/tmp/ccjE1ooy.o:test.cpp:(.text._ZN1AD2Ev[_ZN1AD5Ev]+0xc3): more undefined references to `A::allA' follow
collect2: ld returned 1 exit status


Minimal Code example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//test.cpp
#include <vector>
class A
{
	public:
		static std::vector<A*> allA;
		A()
		{
			allA.push_back(this);
		}
		
		~A()
		{
			for(int i=0; i<allA.size(); i++)
			{
				if(allA[i] == this)
				{
					delete allA[i];
					allA.erase(allA.begin() + (i-1));
					break;
				}
			}
		}
};

int main()
{
	A a1;
	return 0;
}
You shall define your vector. For example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <vector>

class A
{
    // your class definition
};

std::vector<A*> A::allA;

int main()
{
	A a1;
	return 0;
}
Thanks vlad, I forgot that I had to define it outside of the class.
Topic archived. No new replies allowed.