c++ - 获取访问冲突阅读位置但不确定原因

标签 c++

我正在处理 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++;
    }

}

The can't reads I'm getting.

Shows correct here though.

根据要求。这是 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/

相关文章:

c++ - 使用 C++ Windows 应用程序加载 Linux 格式的文本文件

c++ - Dr.Memory 发现的错误 : don't know how to fix it

c++ - 有没有办法(在 C++ 中)创建一个实现某些功能的模板类?

c++ - 如何自定义setRandom的范围?

c++ - 通过引用传递空 QString

具有继承的 C++ 模板部分特化

c++ - Rihedit 文本边距

c++ - 纯转发的成语

c++ - 用基于范围的 for 循环填充指针 vector

c++ - libmysqlcppconn.so.5 : cannot open shared object file: No such file or directory