矢量迭代器不可解引用-C++vs2010

vector iterator not dereferencable - C++ vs2010

本文关键字:引用 -C++vs2010 不可解 迭代器      更新时间:2023-10-16

我有这个问题,不知道该怎么办。有人能帮帮我吗?

void ReadFile(vector<ATTR> &attrVect,MyMatrixDataType &trainMatrix, MyMatrixDataType &testSet)
{
    fstream file_op("gilad.txt",ios::in);
    string sLine = "", first="" ; 
    vector<int>::iterator ptr ; 
    int i ; 
    vector<string>::iterator ptrSV ;

    getline(file_op,sLine) ; 
    // building vector attrVect with all the attribute names and values
    while (sLine != "<END_ATTR>")
    {
        ATTR Temp ; 
        Temp.attrName=split(Temp.values,sLine,1) ;
         if (Temp.values.empty())
        {
            Temp.values.push_back("yes");
            Temp.values.push_back("no");
        } 
        attrVect.push_back(Temp) ; 
        getline(file_op,sLine) ;
    }
    ptrSV=attrVect.back().values.begin() ; 
    for (; ptrSV < attrVect.back().values.end() ; ptrSV++)
         *ptrSV=" " + *ptrSV ; 

    // building The Train Set from File 
    vector<int> *tempMat = new vector<int>[attrVect.size()] ;
    getline(file_op,sLine) ; 
    while (sLine != "<END_TRAIN>")
    {
        vector <string> strVect ; 
        split(strVect,sLine,1) ; 
        for (ptrSV = strVect.begin(), i=0 ; ptrSV < strVect.end() ; ptrSV ++, i++)
            tempMat[i].push_back(attrVect[i].find(*ptrSV)) ; 
        getline(file_op,sLine) ;
    }

    // trasnfering the Train set into a 0,1,2 Matrix representation for easier handling 
    int xSize=attrVect.size(),counter=tempMat[0].size(),j ; 
    MyMatrixDataType::size_type rows = xSize ;
    MyMatrixDataType::size_type cols = counter ;
    MyMatrixDataType a(rows, vector<int>(cols,-2));
    for (i=0 ; i < xSize ; i++)
        for (j=0, ptr=tempMat[i].begin() ; j < counter ; j++, ptr++)            
            ***a[i][j]=*ptr ;*** 
    trainMatrix=a ; 
    delete []tempMat ;
    // building The test Set from File  
    tempMat = new vector<int>[attrVect.size()] ;
    getline(file_op,sLine) ; 
    while (!sLine.empty())
    {
        vector <string> strVect ; 
        split(strVect,sLine,1) ; 
        for (ptrSV = strVect.begin(), i=0 ; ptrSV < strVect.end() ; ptrSV ++, i++)
            tempMat[i].push_back(attrVect[i].find(*ptrSV)) ; 
        getline(file_op,sLine) ;
    }
    xSize=attrVect.size() ; 
    counter=tempMat[0].size(); 
    rows = xSize ;
    cols = counter ;
    MyMatrixDataType b(cols,vector<int>(rows,-2));
    for (i=0 ; i < xSize ; i++)
        for (j=0, ptr=tempMat[i].begin() ; j < counter ; j++, ptr++)            
            b[j][i]=*ptr ;
    testSet=b ; 
    delete []tempMat ; 

    file_op.close() ; 
}

没有测试这是否是唯一的问题,但您必须确保迭代器不等于末尾:

ptrSV != strVect.end()

你做错了:

ptrSV < strVect.end()

这将尝试取消引用两者(ptrSVend()),然后运行元素类的operator <,这将失败,因为end()不是有效的引用(本质上就像试图访问空指针)。

编辑:这部分似乎也错了(分号后面的星号是怎么回事?):

for (i=0 ; i < xSize ; i++)
    for (j=0, ptr=tempMat[i].begin() ; j < counter ; j++, ptr++)
        ***a[i][j]=*ptr ; ***
trainMatrix=a ; 

"vector迭代器不可取消引用"的意思是"您试图取消引用一个无效的迭代器。"至于是哪一行导致了它,我们需要知道它发生的行。你能指出吗?