Im無法訪問以下矢量。我對向量新來說,所以這可能是一個小的語法錯誤。這裏是代碼....訪問指向矢量的指針C++
void spellCheck(vector<string> * fileRead)
{
string fileName = "/usr/dict/words";
vector<string> dict; // Stores file
// Open the words text file
cout << "Opening: "<< fileName << " for read" << endl;
ifstream fin;
fin.open(fileName.c_str());
if(!fin.good())
{
cerr << "Error: File could not be opened" << endl;
exit(1);
}
// Reads all words into a vector
while(!fin.eof())
{
string temp;
fin >> temp;
dict.push_back(temp);
}
cout << "Making comparisons…" << endl;
// Go through each word in vector
for(int i=0; i < fileRead->size(); i++)
{
bool found = false;
// Go through and match it with a dictionary word
for(int j= 0; j < dict.size(); j++)
{
if(WordCmp(fileRead[i]->c_str(), dict[j].c_str()) != 0)
{
found = true;
}
}
if(found == false)
{
cout << fileRead[i] << "Not found" << endl;
}
}
}
int WordCmp(char* Word1, char* Word2)
{
if(!strcmp(Word1,Word2))
return 0;
if(Word1[0] != Word2[0])
return 100;
float AveWordLen = ((strlen(Word1) + strlen(Word2))/2.0);
return int(NumUniqueChars(Word1,Word2)/ AveWordLen * 100);
}
的錯誤是在線路
if(WordCmp(fileRead[i]->c_str(), dict[j].c_str()) != 0)
和
cout << fileRead[i] << "Not found" << endl;
的問題似乎是,因爲它在一個指針的形式用於訪問它的當前語法無效。
爲什麼你不通過引用而不是指針? – Mat