2014-07-22 76 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

相关问题