我遇到了以下问题,我没有收到任何警告或错误,只是程序崩溃。
当我在初始化printAll()
后在我的菜单中运行时。dummyData()
我添加一个新学生的那一刻,它就开始循环studentMenu()
.
域.h:
template <class T>
struct Array{
int length;
T * M;
Array( int size ) : length(size), M(new T[size])
{
}
~Array()
{
delete[] M;
}
};
template <class T>
struct Array{
int length;
T * M;
Array( int size ) : length(size), M(new T[size])
{
}
~Array()
{
delete[] M;
}
};
template <class T>
void addStudent(int a, int b, std::string c,Array <T> A){
A.M[a].setStudent(a,b,c);
}
template <class T>
void DummyDataStudents(Array <T> A){
for(int i=1; i<15; i++){
A.M[i].setStudent(i, i,"student");
}
cout<<"Done Students"<<endl;
}
控制器.h
template <class T>
void _addStudent(Array <T> &A){
int a,b;
string c;
cout<<"Enter ID:"<<endl;
cin>>a;
cout<<"Enter Group:"<<endl;
cin>>b;
cout<<"Enter Name:"<<endl;
cin>>c;
addStudent(a,b,c,A);
}
学生.cpp:
void Student::setStudent(int a,int b,string c){
this->ID = a;
this->group = b;
this->name = c;
}
菜单.cpp:
void Menu::mainMenu(Array <Student> &DBst,Array <Assignment> &DBas){
showMainMenu();
int ret = Menu::intInputHandler();
switch(ret){
case 1:studentMenu(DBst,DBas);break;
case 2:assignmentMenu(DBst,DBas);break;
case 3:statsMenu(DBst,DBas);break;
case 4:_printAll(DBst,DBas);break;
case 0:break;
default:cout<<"Wrong option selected!";break;
}
Menu::mainMenu(DBst,DBas);
}
void Menu::studentMenu(Array <Student> &DBst,Array <Assignment> &DBas){
showStudentMenu();
int ret = Menu::intInputHandler();
switch(ret){
case 1:_addStudent(DBst);break;
case 2:break;
case 3:break;
case 4:break;
case 5:_printAllStud(DBst);break;
case 6:break;
case 0:mainMenu(DBst,DBas);break;
default:cout<<"Wrong option selected!";break;
}
Menu::studentMenu(DBst,DBas);
}
主要.cpp:
int main(){
//Main function of the program
DummyDataStudents(DBst);
DBst.M[10].setStudent(1,10,"sadasd"); //works just fine
DBst.M[10].printStudent();cout<<endl;
Menu main;
main.mainMenu(DBst,DBas);
DBst.~Array();
DBas.~Array();
return 0;
}
///我想这应该足够理解了。当我添加一个新学生时,我会循环打印菜单,直到它崩溃;如果我打电话printall()
,我会直接崩溃。请帮助并为长代码感到抱歉。