C++ STL 向量迭代器...但出现运行时错误

发布于 2024-08-27 16:52:41 字数 1018 浏览 4 评论 0原文

我正在学习STL并制作了win32项目..

但是我陷入了运行时错误..

我尝试调试它但是..

(部分代码)

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;

点击查看调试图片,

这很奇怪,因为

在监视表中,

定义了n1,p1,但没有定义n2 tmp 也不是..

我找不到问题所在...

请帮助..

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..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

您应该更清楚地了解您的问题是什么...

如果您想知道为什么 n1tmp 的值无法显示在调试器,我猜这是因为您正在调试发布版本(或某种带有优化的版本),并且编译器可能已经在执行流程中的那个点“优化掉”了这些变量(它决定它们不是“不再使用或者可以在其他地方获得它们的值)。

尝试调试未优化的构建。

顺便说一句,错误 CXX0017(这是调试器为这些变量显示的内容)意味着“表达式求值器错误”。

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".

淡紫姑娘! 2024-09-03 16:52:41

你能保证多边形总是至少有3个点吗?另外,请仔细查看您的第一个 else if。你还记得 end() 是结束后的一个吗? (你看起来大多是这样,但可能有些地方出了问题......)

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...)

ˇ宁静的妩媚 2024-09-03 16:52:41

请注意:

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

这绝对不应该发生。

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

这看起来有点毫无意义。

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.

伪装你 2024-09-03 16:52:41

您仍然对 currPoly.end() 没有指向有效元素的想法感到困惑。 end() 指向最后一个有效元素之后的一个。

因为您将向量用作环,所以在向量中使用整数索引实际上是编写代码的更好方法。您可以根据向量大小修改索引,而不是编写特殊情况代码。但既然你这样做是为了学习 STL,我们将坚持使用迭代器。这是我认为正确的代码应该是这样的(我没有编译它来看看它是否有效):

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

这是我认为基于索引的版本应该是什么样的(同样,没有用编译器检查它):

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

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文