2009-01-24 81 views
1

我挑出一个运行时错误在这行我的代码:C++为什么此向量访问给出运行时错误?

for (synsAuxCopyIndex=1; synsAuxCopyIndex<synsAux.size(); synsAuxCopyIndex++) 

这是pushSynonyms(string synline, vector<WordInfo> &wordInfoVector)函数内部runnhing。我不明白为什么这个特定行会产生错误,因为我认为我没有索引任何超出范围的东西。

的调试器说:

Uncontrolled Exception 0x00411cbf in program.exe: 0xC0000005: Infracción de acceso al leer la ubicación 0x00000000. 

我猜“Infracción德acceso”将在英国调试器未经授权的访问翻译。

输入文件是

dictionary.txt

1 cute 
2 hello 
3 ugly 
4 easy 
5 difficult 
6 tired 
7 beautiful 
synonyms 
1 7 
7 1 
antonyms 
1 3 
3 1 7 
4 5 
5 4 
7 3 

#include <iostream> 
#include <fstream> 
#include <string> 
#include <sstream> 
#include <vector> 

using namespace std; 

class WordInfo{ 
public:     
    WordInfo() {} 

    ~WordInfo() {} 

    int id() const { return myId; } 

    void readWords(istream &in) { 
    in >> myId >> word;  
    } 

    vector<int>& getSynonyms() { 
    return mySynonyms; 
    } 

    vector<int>& getAntonyms() { 
    return myAntonyms; 
    } 

    string getWord() { 
    return word; 
    } 

    void pushSynonyms (string synline, vector<WordInfo>& wordInfoVector) { 
    stringstream synstream(synline); 
    vector<int> synsAux; 
    int num; 
    while (synstream >> num) 
     synsAux.push_back(num); 
    int wordInfoVectorIndex; 
    int synsAuxCopyIndex; 
    for (wordInfoVectorIndex=0; 
     wordInfoVectorIndex < wordInfoVector.size(); 
     wordInfoVectorIndex++) { 
     if (synsAux[0] == wordInfoVector[wordInfoVectorIndex].id()) { 
     // this is the line that's generating a Runtime Error, Why?              
     for (synsAuxCopyIndex = 1; 
      synsAuxCopyIndex < synsAux.size(); 
      synsAuxCopyIndex++) { 
//  wordInfoVector[wordInfoVectorIndex].mySynonyms.push_back(
//   synsAux[synsAuxCopyIndex]); 
     }               
     }  
    } 
    } 

    void pushAntonyms (string antline, vector<WordInfo> wordInfoVector) { 
    stringstream antstream(antline); 
    vector<int> antsAux; 
    int num; 
    while (antstream >> num) 
     antsAux.push_back(num); 
// for (int i=0; i<antsAux.size(); i++){ 
//  cout<<antsAux[i]<<endl; 
// } 
    } 

    //--dictionary output function 
    void printWords (ostream &out) { 
    out<<myId<< " "<<word;  
    } 

    //--equals operator for String 
    bool operator == (const string &aString) const { 
    return word ==aString; 
    } 

    //--less than operator 
    bool operator < (const WordInfo &otherWordInfo) const { 
    return word<otherWordInfo.word; 
    } 

    //--more than operator 
    bool operator > (const WordInfo &otherWordInfo) const { 
    return word>otherWordInfo.word; 
    } 

public: 
    vector<int> mySynonyms; 
    vector <int> myAntonyms; 

private: 
//vector <int> mySynonyms; 
//vector <int> myAntonyms; 
    string word; 
    int myId; 
}; 

//--Definition of input operator for WordInfo 
istream & operator >> (istream &in, WordInfo &word) { 
    word.readWords(in); 
} 

//--Definition of output operator 
ostream & operator << (ostream &out, WordInfo &word) { 
    word.printWords(out); 
} 

int main() { 
    string wordFile; 
    cout << "enter name of dictionary file: "; 
    getline (cin, wordFile); 
    ifstream inStream(wordFile.data()); 
    if (!inStream.is_open()) { 
    cerr << "cannot open " << wordFile << endl; 
    exit(1);      
    } 

    vector <WordInfo> wordInfoVector; 
    WordInfo aword; 

    while (inStream >> aword && (!(aword == "synonyms"))) 
    wordInfoVector.push_back(aword); 

    inStream.clear(); 

    int i=0;   
    while (i < wordInfoVector.size()) { 
    cout << wordInfoVector[i] << endl; 
    i++; 
    } 

    vector <int> intVector; 
    string synLine; 

    while (getline(inStream, synLine) && (synLine != ("antonyms"))) 
    aword.pushSynonyms(synLine, wordInfoVector); 

    int theIndex; 
    for (theIndex=0; 
     theIndex < wordInfoVector[0].mySynonyms.size(); 
     theIndex++) 
    cout << "the synonyms of 1 are " << 
     wordInfoVector[0].mySynonyms[theIndex] << endl; 
    cout << "the size of mySynonyms of 1 is " << 
     wordInfoVector[0].mySynonyms.size() << endl; 

    for (theIndex=0; 
     theIndex < wordInfoVector[0].mySynonyms.size(); 
     theIndex++) 
    cout << "the synonyms of 7 are " << 
     wordInfoVector[6].mySynonyms[theIndex] << endl; 
    cout << " the size of mySynonyms of 7 is " << 
     wordInfoVector[6].mySynonyms.size() << endl; 

    string antLine; 
    while (getline(inStream, antLine)) 
    aword.pushAntonyms(antLine, wordInfoVector);  

    wordInfoVector[0].mySynonyms.push_back(1); 
    system("PAUSE"); 
    return 0;   
} 

回答

6

如果你添加一些调试输出到您的程序,你会发现你永远不进入,甚至达到“罪魁祸首“for循环。

问题实际上是针对正上方的“synsAux [0]”进行的测试 - synxAux.size()为零,因此访问第零个元素是索引超出界限错误。

相关问题