0

I started learning C++ recently, and when trying some code on my own I got stuck in this problem tha I cant find the solution. I'm sure it's a simple thing, followed some advices from other questions but cant put this simple code to work.

I'm using visual studio 2013.

When compiling, I get the following error:

Error 1 error LNK2001: unresolved external symbol "public: static class std::vector,class std::allocator >,class std::allocator,class std::allocator > > > Config::myVector" (?myVector@Config@@2V?$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) d:\User\andifar\documents\visual studio 2013\Projects\teste\teste\addItem.obj teste

Here's my header file:

#include <vector>

typedef std::vector <std::string> strVector;

class Config
{
public:
    Config();
    static void         addItem();
    static strVector    myVector;
};

and my cpp file:

#include <vector>
#include "addItem.h"

Config::Config(){};

void Config::addItem(){
    Config::myVector.push_back("Hi");
}

main:

#include "addItem.h"

int main(int argc, char** argv)
{   
    Config c;
    c.addItem();
}
yizzlez
  • 8,757
  • 4
  • 29
  • 44
AndiFaria
  • 27
  • 4
  • `static strVector myVector;` is a declaration, and you still need to initialize it. Add `strVector Config::myVector;` to your cpp file. Also guessing this is a duplicate question. – crashmstr Aug 12 '15 at 18:35
  • Also don't forget to `#include `. – aslg Aug 12 '15 at 18:36
  • Or have a look here: http://stackoverflow.com/questions/23983916/cant-set-value-of-static-object-field-error-lnk2001-unresolved-external-symbo – πάντα ῥεῖ Aug 12 '15 at 18:37

0 Answers0