开发者

vector iterator not dereferencable - C++ vs2010 [closed]

It&开发者_如何学C#39;s difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

I have this problem and i don't know what to do. Someone plese help me?

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


Didn't test if that's the only issue but you have to ensure the iterator isn't equal to the end:

ptrSV != strVect.end()

You're doing it wrong:

ptrSV < strVect.end()

This will try to dereference both (ptrSV as well as end()) and then run operator < of the element class which will fail for end() as it isn't a valid reference (it's like trying to access a null pointer essentially).

Edit: This part seems to be wrong as well (what's up with the asterisks behind the semicolon?):

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


"vector iterator not dereferencable" means "You tried to dereference an iterator which is not valid." As to which exact line is causing it, we'd need to know the line where it occurs. Can you indicate that?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜