2014-07-22 139 views
0

嗨我是C++的新手,很難用這段代碼來幫助你排序,因爲我不知道如何解決這些錯誤。 當我運行我的代碼,它拋出了3個錯誤匹配txt文件並計算單詞C++的數量?

on line 116:error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::_Vector_iterator<_Myvec>' (or there is no acceptable conversion) 
1> with 
1> [ 
1> _Myvec=std::_Vector_val<std::_Simple_types<Word>> 
1> ] 
On line 17: could be 'Word &Word::operator =(const Word &)' 
1> while trying to match the argument list '(Word, std::_Vector_iterator<_Myvec>)' 
1> with 
1> [ 
1> _Myvec=std::_Vector_val<std::_Simple_types<Word>> 
1> ] 
On line 120: error C2679: binary '=' : no operator found which takes a right-hand operand of type 'Word' (or there is no acceptable conversion) 
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\vector(390): could be 'std::_Vector_iterator<_Myvec> &std::_Vector_iterator<_Myvec>::operator =(const std::_Vector_iterator<_Myvec> &)' 
1> with 
1> [ 
1> _Myvec=std::_Vector_val<std::_Simple_types<Word>> 
1> ] 

可以請告訴我如何糾正代碼。在幾個部分代碼如下下面

代碼:

class Word 
{ 
public: 
string name; 
int hits; 
Word() 
{ } 
}; 

Update : /***** Prototypes *****/ 

void ReadFileToVector(vector<string> &v, string strFileName); 
void ReadFileToVector(vector<Word> &v, string strFileName); 
void PrintString(string strIn); 
void CompareToBanned(vector<string> &banned, vector<string> &textFile); 
vector<Word> CompareToBanned(vector<Word> &banned, vector<string> &textFile); 
vector<Word> ConvertToWords(vector<Word> arrWords, string strOneWord); 
int main() 
{ 
// Read banned words into a string array 
string strBanned = "banned.txt"; 

Update 2: vector<string> arrBanned; 
vector<Word> arrBannedWords; 
ReadFileToVector(arrBanned, strBanned); 
//for_each(arrBanned.begin(), arrBanned.end(), PrintString); 

string strTextOne = "text1.txt"; 
vector<string> arrTextOne; 
ReadFileToVector(arrTextOne, strTextOne); 

vector<string>::iterator it; 
for(it = arrBanned.begin(); it != arrBanned.end(); it++) 
{ 
    arrBannedWords = ConvertToWords(arrBannedWords, *it); 
} 
CompareToBanned(arrBannedWords, arrTextOne); 

system("pause"); 
} 

void ReadFileToVector(vector<string> &v, string strFileName) 
{ 
    ifstream objFileIn; // Create stream object 
    string strOneWord; 
    objFileIn.open(strFileName); // Open a file and put it into the stream created 

    while(!objFileIn.eof()) 
    { 
    objFileIn >> strOneWord; 
    v.push_back(strOneWord); // For every word in the file push it back to the vector 
    } 
    } 

    void ReadFileToVector(vector<Word> &v, string strFileName) 
    { 
    ifstream objFileIn; // Create stream object 
    string strOneWord; 

    objFileIn.open(strFileName); // Open a file and put it into the stream created 
    Word oneWord; 

    while(!objFileIn.eof()) 
    { 
    objFileIn >> strOneWord; 
    oneWord.name = strOneWord; 
    v.push_back(oneWord); // For every word in the file push it back to the vector 
    } 
} 

void PrintString(string strIn) 
{ 
    cout << strIn << endl; 
} 

void CompareToBanned(vector<string> &banned, vector<string> &textFile) // Take the two files to compare 
{ 
    // For each word in the new text file we need to compare it with every word in the banned list 
    vector<string>::iterator itText; 
    vector<string>::iterator itBanned; 
    int hits = 0; 

    for(itText = textFile.begin(); itText != textFile.end(); itText++) 
    { 
    for(itBanned = banned.begin(); itBanned != banned.end(); itBanned++) 
    { 
    if(*itText == *itBanned) 
    { 
    string foundWord = *itText; 
    hits++; 
    cout << "I have found " << *itBanned << endl; 
    } 
    } 
    } 
    cout << "There were a total of " << hits << " hits in the file." << endl; 

    } 

    vector<Word> CompareToBanned(vector<Word> &banned, vector<string> &textFile) // Take the two files to compare 
    { 
    // For each word in the new text file we need to compare it with every word in the banned list 
    vector<string>::iterator itText; 
    vector<Word>::iterator itBanned; 
    Word oneWord; 
    for(itText = textFile.begin(); itText != textFile.end(); itText++) 
    { 
    for(itBanned = banned.begin(); itBanned != banned.end(); itBanned++) 
    { 
    oneWord = itBanned; // Unable to set a word element of a vector to equal a word 
    if(*itText == oneWord.name) 
    { 
    oneWord.hits++; 
    itBanned = oneWord; 
    cout << "I have found " << oneWord.name << endl; 
    } 
    } 
    } 
    cout << "There were a total of " << oneWord.hits << " hits in the file." << endl; 
    return banned; 
    } 

    vector<Word> ConvertToWords(vector<Word> arrWords, string strOneWord) 
    { 
    Word oneWord; 
    oneWord.name = strOneWord; 
    arrWords.push_back(oneWord); 
    return arrWords; 
    } 
+1

縮進不會增加您的可執行文件的大小,但肯定的,它_will_保存參與調試的一個相當長的時間 – P0W

+0

感謝您的建議,它是在視覺工作室,但當我複製到這裏時丟失了所有的格式! – mattcoder

回答

0

你試圖分配vector<Word>::iterator直接Word對象,反之亦然。你需要從迭代器使用*運算符取消引用Word

oneWord = *itBanned; 

*itBanned = oneWord; 
+0

感謝那個似乎做完了剔的人! :)如果你認爲有更好的做事方式,請提出建議!不夠感謝你! – mattcoder

+0

還有一個問題。該代碼是爲了計算在我的情況下找到的(Hits)9詞,但它實際上顯示-858993457 代碼是從第90行 我做了什麼錯了? 非常感謝 – mattcoder

相關問題