2010-05-11 64 views
0

尝试我创建的代码,当我在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

break;l.erase(it); 迭代器变为无效您it++检查incremante它针对it != l.end() succeds

(*it).name用于比较,但无效,并导致异常