获得访问违规的阅读位置,但不确定为什么

Getting Access violation reading location but not sure why

本文关键字:位置 不确定 为什么 访问      更新时间:2023-10-16

我正在处理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->学生([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 for loop明确初始化构造函数中的所有指针。