开发者

Unwanted debug session

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开发者_如何学C>& 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?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜