blipman17 blipman17 - 10 months ago 53
C++ Question

C++ static vs non static class members

I have a class, let's call it


does not compile when I include the following line in the class in the header.

static std::vector<UnvalidatedSocket*> unvalidatedSockets;

and the following line in the cpp.

Foo::unvalidatedSockets.push_back(new UnvalidatedSocket(ClientSocket));

when I take the
keyword away it compiles just fine.
I checked for circular includes but there are none.

The error is
LNK2001, unresolved external symbol

Why does this happen?
Do static class members get included earlyer on?

Answer Source

You need to initialize your static member somewhere in the CPP file before you try to push something onto it. Add something like this:

std::vector<UnvalidatedSocket*> Foo::unvalidatedSockets;