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

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

概述究竟什么是“断言”,或者更具体地说,我如何摆脱错误.当我创建一个指向带有数据成员int x的类的指针向量,然后执行以下 *** 作: for(I=antiviral_data.begin();I<antiviral_data.end();I++){ if((*I)->x>maxx) { 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 documents\O.exe file:
…include\vector 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 documents\O.exe file:
…include\vector 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,在该页面上搜索无效迭代器.

总结

以上是内存溢出为你收集整理的c – 为什么我的矢量代码断言?无论如何,断言是什么?全部内容,希望文章能够帮你解决c – 为什么我的矢量代码断言?无论如何,断言是什么?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/langs/1222124.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存