题目描述我的代码在运行一部分后会意外终止,我知道是内存出现了问题,但到底是哪句话出了错?这是我们学校的一道练习实验题相关代码#ifndefADDRESS_BOOK_H#defineADDRESS_BOOK_H#includeusingstd::cin;usingstd::cout;usingstd::endl;usingstd::string;structaddress{stringname;longlongphone_number;longlongclassroom;longlongdormitory;};classbook{protected:address*a=NULL;intlistsize;intarraylength;public:book();book(constbook&a);//复制构造函数;~book();voidinsert(book&a);void_delete(book&a,stringc);voidcompile(book&a,stringc);boolfind(constbook&a,stringc);voidprint(constbook&a,intn);};#endifbook::book(){listsize=0;arraylength=50;a=newaddress[arraylength];for(inti=0;i>b.a[listsize].phone_number>>b.a[listsize].classroom>>b.a[listsize].dormitory;listsize++;}voidbook::_delete(book&b,stringc){for(inti=0;i>n1;b.a[i].phone_number=n1;}elseif(n==2){intn2;cin>>n2;b.a[i].classroom=n2;}elseif(n==3){intn3;cin>>n3;b.a[i].dormitory=n3;}}}}boolbook::find(constbook&b,stringc){inti;for(i=0;ic;b.compile(b,c);}elseif(nn2==3){cout
2 回答
慕哥9229398
TA贡献1877条经验 获得超6个赞
析构函数有问题book::~book(){for(inti=0;i{ delete[]a;}}不能循环执行,执行完第一次,a的地址已经无效了。改成book::~book(){delete[]a;}
添加回答
举报
0/150
提交
取消