vector iterator not dereferencable - C ++ vs2010

时间:2011-05-31 23:36:18

标签: c++ debugging

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

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() ; 
}

2 个答案:

答案 0 :(得分:8)

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

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 ; 一个有效的引用(就像试图基本上访问空指针一样)。

编辑: 这部分似乎也是错误的(分号背后的星号是什么?):

{{1}}

答案 1 :(得分:1)

“vector iterator not dereferencable”表示“您试图取消引用无效的迭代器”。至于导致它的确切线,我们需要知道它出现的线。你能指明一下吗?