Я знаю, что существуют очень похожие вопросы.Однако я попытался определить эти переменные в моем файле .cpp
(даже создавая пространство имен для проекта и пытаясь таким образом определить переменные в том же пространстве имен), и я все еще получаю ошибку LNK2001: Unresolved external symbol
.Вот соответствующие части моего кода:
Word.h
#pragma once
#include <string>
#include <vector>
namespace GTW
{
class Word
{
private:
std::string word;
std::string scrambledWord;
static unsigned int numWords;
static std::vector<std::string> allWords; // A vector to store all the words in
static std::vector<std::string> usedWords; // Stores the words already used
void ScrambleWord();
template<typename _T>
bool vecContains(std::vector<_T>& checkVec, _T& element) const;
static int RandomNumber(unsigned int startNum, unsigned int endNum);
public:
Word();
/* GETTERS */
std::string getWord() const;
std::string getScrambledWord() const;
unsigned int getNumWords() const;
std::vector<std::string> getAllWords() const;
};
}
Word.cpp
#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>
#include <vector>
#include <random>
#include "Word.h"
using namespace std;
namespace GTW
{
/* This is my attempt at defining the static variables */
unsigned int Word::numWords;
vector<string> Word::allWords;
vector<string> Word::usedWords;
void Word::ScrambleWord()
{
// Scrambles word by setting this->scrambledWord
return;
}
template<typename _T>
bool Word::vecContains(vector<_T>& checkVec, _T& element) const
{
typename vector<_T>::iterator it;
it = find(checkVec.begin(), checkVec.end(), element);
if (it != checkVec.end()) { return true; }
else { return false; }
}
int Word::RandomNumber(unsigned int startNum, unsigned int endNum)
{
// Returns random number
}
Word::Word()
{
if (Word::allWords.empty()) // Makes sure vector is only filled once
{
// Reads a bunch of Word::allWords from a file into the "Word::allWords" vector
ifstream wordList("words.dat");
if (wordList.is_open())
{
while (getline(wordList, word)) { Word::allWords.push_back(word); }
wordList.close();
}
}
// Stores the number of Word::allWords in the vector
Word::numWords = static_cast<int>(Word::allWords.size());
int randNum = 0;
// Starts a loop that will repeat if the new word has already been used
do
{
// Value for random index
randNum = RandomNumber(0, Word::numWords - 1);
// Sets word to a random index in the vector "Word::allWords"
word = Word::allWords.at(randNum);
} while (vecContains<string>(Word::usedWords, word));
// Scrambles the word
ScrambleWord();
// Adds "word" to Word::usedWords vector
Word::usedWords.push_back(word);
}
/* GETTERS */
string Word::getWord() const { return word; }
string Word::getScrambledWord() const { return scrambledWord; }
unsigned int Word::getNumWords() const { return Word::numWords; }
vector<string> Word::getAllWords() const { return Word::allWords; }
}
Полное сообщение об ошибке:
1>Word.obj : error LNK2001: unresolved external symbol "private: static unsigned int Word::numWords" (?numWords@Word@@0IA)
1>Word.obj : 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> > > > Word::allWords" (?allWords@Word@@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)
1>Word.obj : 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> > > > Word::usedWords" (?usedWords@Word@@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)
1>C:\Users\username\Dev\GuessTheWord\bin\Debug-x64\GuessTheWord\GuessTheWord.exe : fatal error LNK1120: 3 unresolved externals
1>Done building project "GuessTheWord.vcxproj" -- FAILED.