尝试我创建的代码,当我在Borland C ++中使用它并尝试使用remove-function调试会话时,它会指向一个名为“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-----------------------------------------
为什么会显示此错误,我该如何解决?
答案 0 :(得分:0)
在break;
后执行l.erase(it);
迭代器变为无效你通过it++
检查it != l.end()
成功的
(*it).name
用于比较,但无效并导致异常