我正在处理 C++ 类作业。我收到访问冲突读取位置 0xCDCDCDCDCD。我不知道是什么。
这是定义指针数组的花名册构造函数。根据说明,它必须是指针数组。
roster::roster(int capacity)
{
this->capacity = capacity;
this->lastIndex = -1;
this->students = new student*[capacity];
}
这是我的添加方法。在调试中,所有值都是正确的。即使当我给新学生上课时,它似乎就在那里,但在超过那个点后它有一个不同的指针地址。
void roster::add(string studentId, string firstName, string lastName, string emailAddress, int age, int daysInCourse1, int daysInCourse2, int daysInCourse3, Degree degreeType)
{
int openArrayIndex = 0;
for (int i = 0; i < capacity; i++) {
if (students[i] == NULL) {
break;
}
else {
openArrayIndex++;
}
}
int daysInCourses[3]{ daysInCourse1, daysInCourse2, daysInCourse3 };
switch (degreeType)
{
case SECURITY: {
students[openArrayIndex] = new securityStudent(studentId, firstName, lastName, emailAddress, age, daysInCourses, degreeType);
break;
}
case SOFTWARE: {
students[openArrayIndex] = new softwareStudent(studentId, firstName, lastName, emailAddress, age, daysInCourses, degreeType);
break;
}
case NETWORK: {
students[openArrayIndex] = new networkStudent(studentId, firstName, lastName, emailAddress, age, daysInCourses, degreeType);
break;
}
}
this->lastIndex = openArrayIndex;
}
这是在 (this->students)[i]->print(); 上实际发生异常的地方;
void roster::printAll()
{
int currentArrayIndex = 0;
for (int i = 0; i <= lastIndex; i++) {
if ((this->students)[i] != NULL) {
(this->students)[i]->print();
}
currentArrayIndex++;
}
}
根据要求。这是 student.cpp 和 secuirityStudent.cpp。
student::student()
{
this->studentId = "";
this->firstName = "";
this->lastName = "";
this->emailAddress = "";
this->age = 0;
for (int i = 0; i < daysInCoursesArrSize; i++)
this->daysInCourses[i] = 0;
}
student::student(string studentId, string firstName, string lastName, string emailAddress, int age, int daysInCourses[])
{
this->studentId = studentId;
this->firstName = firstName;
this->lastName = lastName;
this->emailAddress = emailAddress;
this->age = age;
for (int i = 0; i < daysInCoursesArrSize; i++)
this->daysInCourses[i] = daysInCourses[i];
}
void student::SetStudentId(string studentId) { this->studentId = studentId; }
string student::GetStudentId() { return studentId; }
void student::SetFirstName(string firstName) { this->firstName = firstName; }
string student::GetFirstName() { return firstName; }
void student::SetLastName(string lastName) { this->lastName = lastName; }
string student::GetLastName() { return lastName; }
void student::SetEmailAddress(string emailAddress) { this->emailAddress = emailAddress; }
string student::GetEmailAddress() { return emailAddress; }
void student::SetAge(int age) { this->age = age; }
int student::GetAge() { return age; }
void student::SetDaysInCourses(int daysInCourses[]) {
for (int i = 0; i < daysInCoursesArrSize; i++)
this->daysInCourses[i] = daysInCourses[i];
}
int * student::GetDaysInCourses() { return daysInCourses; }
void student::print() {
cout << left << setw(10) << studentId;
cout << left << setw(20) << firstName;
cout << left << setw(20) << lastName;
cout << left << setw(30) << emailAddress;
cout << left << setw(10) << age;
cout << left << setw(10) << daysInCourses[0];
cout << left << setw(10) << daysInCourses[1];
cout << left << setw(10) << daysInCourses[2];
}
student::~student()
{
}
securityStudent::securityStudent() :student()
{
degreeType = SECURITY;
}
securityStudent::securityStudent(string studentId, string firstName, string lastName, string emailAddress, int age, int daysInCourses[], Degree degreeType)
: student(studentId, firstName, lastName, emailAddress, age, daysInCourses)
{
degreeType = SECURITY;
}
Degree securityStudent::GetDegreeType() { return SECURITY; }
void securityStudent::print() {
this->student::print();
cout << degreeTypeStrings[degreeType] << "\n";
}
securityStudent::~securityStudent()
{
student::~student();
}
最佳答案
尝试使用 for 循环在构造函数中显式初始化所有指向 null 的指针。
关于c++ - 获取访问冲突阅读位置但不确定原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57237716/