简体   繁体   中英

C++ STL vector iterator… but got runtime error

I'm studying STL and made win32 project..

But I got stuck in runtime error..

I tried to debug it but..

(partial code)

vector<Vertex> currPoly=polygons.back();
vector<Vertex>::iterator it;


for(it=currPoly.begin();it!=currPoly.end();++it){
    vector<Vertex>::iterator p1;
    vector<Vertex>::iterator n1;
    vector<Vertex>::iterator n2;

    if(         it==currPoly.begin()){
        p1=currPoly.end();
        n1=it+1;
        n2=it+2;
    }else if(   it==currPoly.end()-1){
        p1=it-1;
        n1=it+1;
        n2=currPoly.begin();
    }else if(   it==currPoly.end()){
        p1=it-1;
        n1=currPoly.begin();
        n2=currPoly.begin()+1;
    }else{
        p1=it-1;
        n1=it+1;
        n2=it+2;
    }
    int tmp;
    tmp=it->x;
    tmp=p1->x;

please click to see debugging picture

this is very strange because

in watch table,

n1,p1,it are defined but n2 isn't and tmp is not either..

I can't find what is wrong...

please help..

You should be a little more clear about exactly what your question is...

If it's that you're wondering why values for n1 and tmp can't be displayed in the debugger, I'm guessing that it's because you're debugging a release build (or some kind of build with optimizations), and the compiler has probably 'optimized away' those variables by that point in the execution flow (it decided they weren't used anymore or their values could be obtained elsewhere).

Try debugging a non-optimized build.

By the way, error CXX0017 (which is what the debugger is displaying for those variables) means, "Expression Evaluator Error".

Are you guaranteed that a polygon always has at least 3 points? Also, have a close look at your first else if. Are you remembering that end() is one past the end? (You mostly seem to, but there may be/are places where something's going wrong with that...)

As a note:

}else if(   it==currPoly.end()){
    p1=it-1;
    n1=currPoly.begin();
    n2=currPoly.begin()+1;
}

This should never happen.

int tmp;
tmp=it->x;
tmp=p1->x;

This looks a bit pointless to do.

You're still having trouble with the idea that currPoly.end() does not point to a valid element. end() points to one after the last valid element.

Because you are using the vector as a ring, using integer indices into the vector would actually be a better way to write your code. You could mod indices against the vector size instead of writing special case code. But since you're doing it to learn STL, we'll stick with iterators. Here's what I think the correct code should look like (I didn't compile it to see if it works):

vector<Vertex> currPoly=polygons.back();
vector<Vertex>::iterator it;


for(it=currPoly.begin();it!=currPoly.end();++it){
    vector<Vertex>::iterator p1;
    vector<Vertex>::iterator n1;
    vector<Vertex>::iterator n2;

    if(it==currPoly.begin()){
        p1=currPoly.end()-1;
        n1=it+1;
        n2=it+2;
    }else if(it==currPoly.end()-1){
        p1=it-1;
        n1=currPoly.begin();
        n2=n1+1;
    }else if(it==currPoly.end()-2){
        p1=it-1;
        n1=it+1;
        n2=currPoly.begin();
    }else{
        p1=it-1;
        n1=it+1;
        n2=it+2;
    }
    int tmp;
    tmp=it->x;
    tmp=p1->x;
}

And here's what I think the index-based version would look like (again, didn't check it with a compiler):

vector<Vertex> currPoly=polygons.back();


for(int i=0; i < currPoly.size(); ++i){
    int p1 = (i+currPoly.size()-1)%currPoly.size();
    int n1 = (i+1)%currPoly.size();
    int n2 = (i+2)%currPoly.size();

    int tmp;
    tmp=currPoly[i].x;
    tmp=currPoly[p1].x;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM