0

Error message in question:

LNK2001 unresolved external symbol "public: static class std::vector<struct MakeKey::KeyStruct,class std::allocator<struct MakeKey::KeyStruct> > MakeKey::KeyArray" (?KeyArray@MakeKey@@2V?$vector@UKeyStruct@MakeKey@@V?$allocator@UKeyStruct@MakeKey@@@std@@@std@@A)

Hello i'm kind of new to this whole coding thing and apologize if the error is stupid but I am getting a unresolved external symbol error on both of my public static vectors. I have read through multiple posts on stack overflow and can't seem to figure out what I am doing wrong. Anyway here is the minimal code that produces the same errors. Yes it is important that the vectors remain static because multiple other errors appear if it is non-static.

class MakeKey
{
public:
    typedef struct KeyStruct {
        sf::Image Img;
        sf::Texture Tex;
        sf::Sprite Sprite;
    }NewKey;
    static vector <MakeKey::NewKey> KeyArray;
    static vector <sf::RenderWindow*> WindowArray;
    static void StepWindows()
    {
        for (int i{ 0 }; i > MakeKey::KeyArray.size(); i++)
        {
            WindowArray[i]->clear(sf::Color::Transparent);
            WindowArray[i]->draw(KeyArray[i].Sprite);
            WindowArray[i]->display();
        }
    }
};

And my main

int main()
{
    MakeKey::StepWindows();
}
JohnFilleau
  • 4,045
  • 1
  • 15
  • 22
Jacob
  • 213
  • 1
  • 3
  • 11
  • 1
    "Hello i'm kind of new to this whole coding thing" + "Yes it is important that the vectors remain static because multiple other errors appear if it is non-static" makes me think this could be an instance of you using magic C++ voodoo keywords to get the compiler to shut up. – JohnFilleau Mar 22 '20 at 01:37
  • First off I don't think you could have phrased that in a funnier way, but in a serious answer if it is non-static you have to declare it in the void as ```MakeKey WindowArray WindowArray;``` and if I do that then i get an error every time I try to use [i]. By you have too I mean I think. – Jacob Mar 22 '20 at 01:43
  • Read this in the meantime until you get that text in the question. https://en.cppreference.com/w/cpp/language/static edit: thanks for the edit. Text in the question is best :D – JohnFilleau Mar 22 '20 at 01:47
  • [this](https://stackoverflow.com/questions/195207/unresolved-external-symbol-on-static-class-members) is a duplicate that I think is very well written. `static` members of classes are not definitions - they're not instantiated until you explicitly instantiate them. Usually at the global space in the associated `.cpp` file. – JohnFilleau Mar 22 '20 at 01:51
  • If your `MakeKey` class is in a `.h` file, you'd put the definition for the member in the associated `.cpp` file. If there's no `.cpp` file associated with that `.h` file, you stick it in the global namespace in the same `.cpp` file as `main()`. – JohnFilleau Mar 22 '20 at 01:52
  • Yup that answers my question thanks – Jacob Mar 22 '20 at 04:18

0 Answers0