Search code examples
c++visual-c++staticclass-members

C++ static vs non static class members


I have a class, let's call it Foo Foo 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 statickeyword 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?


Solution

  • 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;