我有一個字典類,用於拼寫檢查。我有一個數組作爲單詞列表,我必須用一個有文字的文件來初始化它。 我的問題是,我需要我的wordlist變量是一個靜態變量,導致只有一個是足夠的從字典類創建任何其他額外的對象,這是合乎邏輯的,但是不需要第二個對象的類,但如果我們需要多個對象呢?有沒有辦法?使用文件初始化靜態成員
#ifndef DICTIONARY_H
#define DICTIONARY_H
class Dictionary
{
public:
static const int SIZE = 109582;
Dictionary();
bool lookUp(const char *)const;
private:
void suggestion(const char *)const;
char *wordList[SIZE];
};
#endif
單詞表必須是靜態的......
我只能認爲這種定義的...
Dictionary::Dictionary()
{
ifstream inputFile("wordsEn.txt", std::ios::in);
if (!inputFile)
{
cerr << "File could not be opened." << endl;
throw;
}
for (int i = 0; i < SIZE && !inputFile.eof(); ++i)
{
wordList[i] = new char[32];
inputFile >> wordList[i];
}
}
這個主題可能有一些其他的變化,但是,這就是你必須做的。它需要在循環中讀取文件並放入列表中。 –
這是真的,但它是浪費時間和記憶,有多個詞列表@MatsPetersson – shayan
我不明白爲什麼你需要多個字典,反正。 –