不需要的调试会话

发布于 2024-08-31 17:43:43 字数 2646 浏览 7 评论 0原文

尝试我创建的这段代码,当我在 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 技术交流群。

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

发布评论

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

评论(1

以可爱出名 2024-09-07 17:43:43

l.erase(it); 之后执行 break;
迭代器变得无效,您通过 it++ 递增它检查 it != l.end() 成功

(*i​​t).name 用于比较但无效并导致异常

do a break; after l.erase(it);
iterator becomes invalid you incremante it by it++ check against it != l.end() succeds

(*it).name is used for comparison but is invalid and causes exception

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