0

I'm struggling with unresolved token and unresolved external symbol. I decided to leave the names of classes and variables as they are because I believe this will be easier this way to understand what I'm trying to do.

QuestionsSet.h

#include "Question.h"
class QuestionsSet{
private:
    static std::vector<Question> questions;
public:
    static Question getQuestion();
};

QuestionsSet.cpp

#include "QuestionsSet.h"
Question QuestionsSet::getQuestion() {
    for (Question q : questions) {
        if (!q.used) {
            return q;
        }
    }
}

Class Question has a field "used" of type bool, which is false by default and turns true once question is used in the game. I think his is plain C++ yet I put "C++/CLI" in tags because this is a part of application using Windows Forms and maybe this matters in this case. I'm getting linker errors on any reference to vector questions.

The errors read:

Error LNK2020 unresolved token (0A00046C) "public: static class std::vector > QuestionsSet::questions" (?questions@QuestionsSet@@2V?$vector@VQuestion@@V?$allocator@VQuestion@@@std@@@std@@A)

Error LNK2001 unresolved external symbol "public: static class std::vector > QuestionsSet::questions" (?questions@QuestionsSet@@2V?$vector@VQuestion@@V?$allocator@VQuestion@@@std@@@std@@A)

Error LNK1120 2 unresolved externals

I've spent hours trying to figure it out but I can't seem to find the solution. I've searched for solutions yet none seemed to apply in this case.

  • Indeed, I changed back S to QuestionsSet as it should be. I changed the code as you said and everything works, yet I don't fully understand why it needs to be done this way. As far as functions are concerned I know that is needs to be specified with :: where the function actually is but I wouldn't expect this behavior of vectors. – tomaszgorol Oct 14 '18 at 13:13
  • Here is the example in the linked duplicate that should help: https://stackoverflow.com/a/12574407/487892 look at the last part of this answer for static data members. – drescherjm Oct 14 '18 at 13:15
  • I missed a : in my example code. That was a typo. It should be `std::vector QuestionsSet::questions;` – drescherjm Oct 14 '18 at 13:18
  • Thank you very much, now I got it. :) – tomaszgorol Oct 14 '18 at 13:19
  • Looks like you *declared* `QuestionsSet::questions` but never *defined* it. Please add a minimal definition of `Question` so that your code becomes a [mcve], and that will confirm or refute this theory. – Toby Speight Oct 19 '18 at 07:41
  • In particular, note the last example from [this answer to the duplicate question](/a/12574407): **static data members must be defined outside the class in a single translation unit:**. – Toby Speight Oct 19 '18 at 07:43

0 Answers0