0

So I have a simple class with an overloaded function operator. I also have a static vector in there. I'm trying to add values to the vector through the function operator, but it doesn't seem to be working.

class ExampleClass
{
private:
    static std::vector<std::string> exampleVector;

public:
    void operator () (std::string input)
    {
        exampleVector.push_back(input);
    }
};

int main()
{
    ExampleClass exampleObject;
    exampleObject("Hello World!");
}

The error I'm getting is a "unresolved external symbol":

error LNK2001: unresolved external symbol "private: static class std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > ExampleClass::exampleVector" (?exampleVector@ExampleClass@@0V?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@std@@A)

I have no clue what what means. But my code seems to work fine without the static keyword. So what am I missing here? Thanks.

Alex
  • 462
  • 1
  • 7
  • 19

1 Answers1

2

You need to define the static data member outside your class as well:

std::vector<std::string> ExampleClass::exampleVector;
cigien
  • 57,834
  • 11
  • 73
  • 112