加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 为什么我的矢量代码断言?无论如何,断言是什么?

发布时间:2020-12-16 10:41:53 所属栏目:百科 来源:网络整理
导读:究竟什么是“断言”,或者更具体地说,我如何摆脱错误.当我创建一个指向带有数据成员int x的类的指针向量,然后执行以下操作: for(I=antiviral_data.begin();Iantiviral_data.end();I++){ if((*I)-xmaxx) { antiviral_data.erase(I); }} 并运行该程序,我得到没
究竟什么是“断言”,或者更具体地说,我如何摆脱错误.当我创建一个指向带有数据成员int x的类的指针向量,然后执行以下操作:

for(I=antiviral_data.begin();I<antiviral_data.end();I++)
{
    if((*I)->x>maxx)
    {
        antiviral_data.erase(I);
    }
}

并运行该程序,我得到没有错误,直到x大于maxx并且我使用.erase(),此时我收到此错误:

Debug Assertion Failed!

Program: …My DocumentsO.exe File:
…includevector Line: 116

Expression:
(“this->_Has_container()”,0)

For information on how your program
can cause an assertion failure,see
the Visual C++ documentation on
asserts.

(Press Retry to debug the application)

[Abort][Retry][Ignore]

另外,如果我尝试使用cout:

cout<<(*antiviral_data.begin())->x<<endl;

我收到此错误:

Debug Assertion Failed!

Program: …My DocumentsO.exe File:
…includevector Line: 98

Expression: vector iterator not
deferencable

For information on how your program
can cause an assertion failure,see
the Visual C++ documentation on
asserts.

(Press Retry to debug the application)

[Abort][Retry][Ignore]

有人可以告诉我为什么我不能使用向量中的任何数据,以及如何解决它?

另外:antiviral_data是指针的向量,只有一个元素:

antiviral_data.push_back(new aX1(player.x,player.y,'>'));

如果这有帮助.

解决方法

你得到断言的最可能的原因是你在擦除后增加了我.试试这个:

for(I=antiviral_data.begin();I!=antiviral_data.end();)
{
    if((*I)->x>maxx) I=antiviral_data.erase(I); else ++I;
}

另请参见http://www.cppreference.com/wiki/stl/vector/erase,在该页面上搜索无效迭代器.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读