不需要的调试会话
尝试我创建的这段代码,当我在 Borland C++ 中使用它并尝试删除功能时,会打开一个调试会话,它指向一个名为“xstring”的文件,并且显示“EAccessViolation”。
它指向文件中的这一行:
return (compare(0, _Mysize, _Right._Myptr(), _Right.size()));
//---------------------------------------------------------------------------
#include<iostream>
#include<string>
#include<fstream>
#include<list>
#pragma hdrstop
using namespace std;
struct Mail{
string name;
string ammount;
};
//---------------------------Call_Functions-----------------------------
void new_mail(list<Mail>& l);
void show_mail(list<Mail> l);
void remove(list<Mail>& l);
//---------------------------------Menu--------------------------------------
#pragma argsused
int main(int argc, char* argv[])
{
list<Mail> mail;
bool contin = true;
char answ;
do{
cout<<'\n'<<'\t'<<'\t'<<"Menu"<<endl
<<'\t'<<'\t'<<"----"<<endl
<<"1. New mail"<<endl
<<"2. Show mail"<<endl
<<"3. Remove mail"<<endl
<<"4. Exit"<<endl<<endl;
cin>>answ;
cin.ignore(1000, '\n');
switch (answ) {
case '1':
new_mail(mail);
break;
case '2':
show_mail(mail);
break;
case '3':
remove(mail);
break;
case '4':
exit(1);
default:
cout<<"Choice not recognized";
}
}
while(contin);
return 0;
}
//------------------------------Functions-------------------------------------
//------------------------------New_mail--------------------------------------
void new_mail(list<Mail>& l){
Mail p;
cout<<endl<<"Type in the name of the new mail ";
getline(cin, p.name);
cout<<"Now type in the cost: ";
getline(cin, p.ammount);
l.push_back(p);
}
//------------------------------Show_Mail-------------------------------------
void show_mail(list<Mail> l){
list<Mail>::iterator it;
cout<<"\nAll mail:\n\n";
for (it = l.begin(); it != l.end(); it++) {
cout<<(*it).name<<'\t'<<'\t'<<(*it).ammount<<endl;
}
}
//------------------------------Remove----------------------------------------
void remove(list<Mail>& l){
list<Mail>::iterator it;
string name;
cout<<endl<<"What is the name of the mail you want to remove?: ";
getline(cin, name);
for (it = l.begin(); it != l.end(); it++) {
if ((*it).name == name) {
l.erase(it);
}
}
}
//------------------------------End-----------------------------------------
为什么它显示此错误,以及如何解决它?
Try this code I created,when I use it in Borland C++ and try the remove-function a debug session opens up and it points to a file called "xstring",and it is saying "EAccessViolation".
it points to this line in the file:
return (compare(0, _Mysize, _Right._Myptr(), _Right.size()));
//---------------------------------------------------------------------------
#include<iostream>
#include<string>
#include<fstream>
#include<list>
#pragma hdrstop
using namespace std;
struct Mail{
string name;
string ammount;
};
//---------------------------Call_Functions-----------------------------
void new_mail(list<Mail>& l);
void show_mail(list<Mail> l);
void remove(list<Mail>& l);
//---------------------------------Menu--------------------------------------
#pragma argsused
int main(int argc, char* argv[])
{
list<Mail> mail;
bool contin = true;
char answ;
do{
cout<<'\n'<<'\t'<<'\t'<<"Menu"<<endl
<<'\t'<<'\t'<<"----"<<endl
<<"1. New mail"<<endl
<<"2. Show mail"<<endl
<<"3. Remove mail"<<endl
<<"4. Exit"<<endl<<endl;
cin>>answ;
cin.ignore(1000, '\n');
switch (answ) {
case '1':
new_mail(mail);
break;
case '2':
show_mail(mail);
break;
case '3':
remove(mail);
break;
case '4':
exit(1);
default:
cout<<"Choice not recognized";
}
}
while(contin);
return 0;
}
//------------------------------Functions-------------------------------------
//------------------------------New_mail--------------------------------------
void new_mail(list<Mail>& l){
Mail p;
cout<<endl<<"Type in the name of the new mail ";
getline(cin, p.name);
cout<<"Now type in the cost: ";
getline(cin, p.ammount);
l.push_back(p);
}
//------------------------------Show_Mail-------------------------------------
void show_mail(list<Mail> l){
list<Mail>::iterator it;
cout<<"\nAll mail:\n\n";
for (it = l.begin(); it != l.end(); it++) {
cout<<(*it).name<<'\t'<<'\t'<<(*it).ammount<<endl;
}
}
//------------------------------Remove----------------------------------------
void remove(list<Mail>& l){
list<Mail>::iterator it;
string name;
cout<<endl<<"What is the name of the mail you want to remove?: ";
getline(cin, name);
for (it = l.begin(); it != l.end(); it++) {
if ((*it).name == name) {
l.erase(it);
}
}
}
//------------------------------End-----------------------------------------
Why does it show this error,and how can I solve it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
l.erase(it);
之后执行break;
迭代器变得无效,您通过
it++
递增它检查it != l.end()
成功(*it).name
用于比较但无效并导致异常do a
break;
afterl.erase(it);
iterator becomes invalid you incremante it by
it++
check againstit != l.end()
succeds(*it).name
is used for comparison but is invalid and causes exception