Skip to main content
 首页 » 编程设计

C++:创建对象时程序崩溃

2024年12月31日28youxin

我创建了以下基本的大学系统类(class)。

当我创建另一个大学类对象时,我正面临崩溃。

第一个工作正常(pmu),由于某种原因,我不知道为什么当我创建另一个对象时程序不会崩溃,正如您在 int main() 末尾看到的那样作为评论。

谢谢你,

class Date 
{ 
public: 
int day; 
int month; 
int year; 
Date(){day=0;month=0;year=0;} 
Date(int d,int m,int y){day=d;month=m;year=y;} 
void Print(){cout<<day<<"-"<<month<<"-"<<year<<endl;} 
}; 
class Student 
{ 
public: 
int id; 
string name; 
double gpa; 
Date dob; 
Student(){id=0;name="";gpa=0;} 
Student(int i,string n,double g, Date d){id=i; name=n; gpa=g; dob=d;} 
void Print(){cout<<"id="<<id<<", name="<<name<<", gpa="<<gpa<<", DOB=";dob.Print();} 
}; 
class Department 
{ 
public: 
string name; 
int numOfStudents; 
string chair; 
Student students[100]; 
Department(){name="";numOfStudents=0;chair="";} 
Department(string n, string c){name=n; chair=c; numOfStudents=0;} 
void Print(){cout<<"Department="<<name<<", # of students="<<numOfStudents<<", Chair="<<chair<<endl;} 
void PrintStudents(){for(int i=0; i<numOfStudents; i++) students[i].Print();} 
void AddStudent(Student s){students[numOfStudents]=s;numOfStudents++;} 
void DeleteStudent(int id){} 
}; 
 
class College 
{ 
public: 
string name; 
int numOfStudents; 
string dean; 
Department departments[10]; 
int numOfDepartments; 
College(){name="";numOfStudents=0;dean="";numOfDepartments=0;} 
College(string n, string d){name=n; dean=d; numOfStudents=0;numOfDepartments=0;} 
void Print(){cout<<"College="<<name<<", # of students="<<numOfStudents<<", Dean="<<dean<<endl;} 
void PrintDepartments(){for(int i=0; i<numOfDepartments; i++) departments[i].Print();} 
void AddDepartment(Department s){departments[numOfDepartments]=s;numOfDepartments++;numOfStudents+=s.numOfStudents;} 
void DeleteDepartment(string name){} 
}; 
class University 
{ 
public: 
string name; 
int numOfStudents; 
string rector; 
College colleges[10]; 
int numOfColleges; 
University(){name="";numOfStudents=0;rector="";numOfColleges=0;} 
University(string n, string r){name=n;rector=r;numOfStudents=0;numOfColleges=0;} 
void Print(){cout<<"University="<<name<<", # of students="<<numOfStudents<<", Rector="<<rector<<endl;} 
void PrintColleges(){for(int i=0; i<numOfColleges; i++) colleges[i].Print();} 
void AddCollege(College c){colleges[numOfColleges]=c;numOfColleges++;numOfStudents+=c.numOfStudents;} 
void DeleteCollege(string name){} 
 
 
}; 
 
int main() 
{ 
cout<<"here is the object date:\n"; 
Date today(2,3,2015); 
today.Print(); 
 
cout<<"here is the object maryam:\n"; 
Student maryam(123,"maryam", 3.5, today); 
maryam.Print(); 
 
cout<<"here is the object department:\n"; 
Department it("IT", "Dr. Loay"); 
it.Print(); 
 
cout<<"here is all the students in the department:\n"; 
it.AddStudent(maryam); 
 
Student sara(225,"sara", 3.5, today); 
it.AddStudent(sara); 
 
Student fatimah(11,"fatimah", 3.5, today); 
it.AddStudent(fatimah); 
 
 
it.PrintStudents(); 
 
 
it.Print(); 
 
College cces("CCES", "Dr. Ammar"); 
cout<<"here is the college information:\n"; 
cces.Print(); 
 
 
 
cces.AddDepartment(it); 
 
cces.PrintDepartments(); 
cout<<"here is the college information:\n"; 
cces.Print(); 
 
University pmu("PMU", "DR. Essa"); 
pmu.AddCollege(cces); 
 
 
cout<<"\nhere is the university information:\n"; 
pmu.Print(); 
pmu.PrintColleges(); 
 
Date today1 (4,2,2015); 
today1.Print(); 
 
Student khalid (1001,"Khalid",3.6,today); 
 
 
Student ahmed (1002,"Ahmed",3.6,today); 
 
 
Student john (1003,"John",3.6,today); 
 
 
Student tom (1004,"Tom",3.6,today); 
 
 
Department MEeng("MEEng","Dr. Nader"); 
MEeng.AddStudent(khalid); 
MEeng.AddStudent(ahmed); 
MEeng.AddStudent(john); 
MEeng.AddStudent(tom); 
MEeng.Print(); 
MEeng.PrintStudents(); 
 
College coneng ("CoEng","Dr. Jamal"); 
coneng.AddDepartment(MEeng); 
coneng.Print(); 
pmu.AddCollege(coneng); 
pmu.Print(); 
pmu.PrintColleges(); 
 
Student raja (2001,"Raja",3.7,today1); 
 
 
Student sultan (2002,"Sultan",3.7,today1); 
 
 
Student nasser (2003,"Nasser",3.7,today1); 
 
 
Student jim (2004,"Jim",3.7,today1); 
 
Department Civil ("Civil", "Dr. Yousef"); 
 
Civil.AddStudent(raja); 
Civil.AddStudent(jim); 
Civil.AddStudent(nasser); 
Civil.AddStudent(sultan); 
 
College eng("Enginerring","Dr. Khalid"); 
 
eng.AddDepartment(Civil); 
cout<<"Depratments of kfupm eng "<<endl; 
eng.PrintDepartments(); 
eng.Print(); 
/*University sa ("KFUPM", "Dr. Waleed"); */// the problem happens when I create another Uni object 
//kfupm.AddCollege(eng); 
 
 
return 0; 
} 

请您参考如下方法:

问题是您超出了堆栈大小的限制。我改变了你的main()程序来简单地做到这一点:

int main() 
{ 
    cout << sizeof(University) << "\n"; 
    cout << sizeof(College) << "\n"; 
    cout << sizeof(Department); 
} 

使用 Visual Studio 2013,打印的内容如下:
567192 
56712 
5664 
University本身的大小超过半兆字节。当您在代码中创建 2 个或更多时,您会超过堆栈内存,Visual Studio 项目默认为 1 兆字节,我猜对于您的环境/编译器,大小相似。

所以解决方案是使用 new 在堆上声明这些对象。 ,或者更好的是,使用 std::vector而不是类中的原始数组。

编辑:来自 ideone 的实时示例: http://ideone.com/gRUxGR

无论哪种方式,这些对象都非常大,您不能简单地将它们声明在堆栈上。

编辑2:
使用 std::vector 时,尺寸急剧减小。这里的代码显示了这一点:

http://ideone.com/mS1gYJ

对于 Visual Studio,输出如下:
80 
80 
72 

此外,使用 std::vector消除了您现在在类里面对无关的“计数”变量的需要,并且不将您限制为只有 100 个学生、10 个部门。