0

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?

blipman17
  • 523
  • 3
  • 23
  • 1
    static members need to be initialized not only declared. Add `std::vector Foo::unvalidatedSocket;` to your cpp file. – Quest Oct 11 '16 at 20:05
  • 1
    Do not tell anybody what error you are getting, that would be much more interesting for people to guess... – Slava Oct 11 '16 at 20:09
  • Darnit.. I really don't like that you have to double declare stuff in c++. (Well, not really double declare.) Seems that there is still stuff a javadevelopper can learn – blipman17 Oct 11 '16 at 20:09
  • @blipman17 I really do not like when people do not ask questions properly, for example telling about errors and not showing them, calling linker errors "it does not compile" etc – Slava Oct 11 '16 at 20:13
  • dumb error from me, Added the error to the description of the problem. – blipman17 Oct 11 '16 at 20:15

1 Answers1

1

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;
Brick
  • 3,998
  • 8
  • 27
  • 47