Ошибка оператора копирования

0

Я пытаюсь сделать оператор назначения копии. Но это не сработает. В чем проблема? есть ли другой способ написать оператор присваивания копии?

Course&  Course::operator= ( const Course &that)
{
    if (this != &that)
    {
        courseId = that.courseId; // in this line I'm getting run-time error.
        courseName = that.courseName;
        gradeFormLength = that.gradeFormLength;
        studentsLength = that.studentsLength;

        delete[] gradeForms;
        gradeForms = new GradeForm[that.gradeFormLength];
        for(int i = 0; i < that.gradeFormLength; i++)
        {
            gradeForms[i] = that.gradeForms[i];
        }

        delete[] students;
        students = new Student[studentsLength];

        for(int i = 0; i < that.studentsLength; i++)
        {
            students[i] = that.students[i];
        }
    }
    return *this;
}

Здесь вызывается оператор =.

void StudentReviewSystem::deleteCourse(const int courseId)
{
    int index = findCourse(courseId);

    if(index != -1)
    {
        int newNum = numberOfCourses-1;
        Course *newCourses = new Course[newNum];
        int k;
        for(int j = 0; j < newNum; j++)
        {
            if(courses[j].getId() == courseId)
                k++;
            newCourses[j] = courses[k]; // <<< there
            k++;
        }
        delete[] courses;
        courses = newCourses;
        numberOfCourses = newNum;
        cout<< "Course "<< courseId <<" has been deleted."<< endl;
    }

    else
    {
        cout<< "Course "<< courseId <<" doesn't exist."<< endl;
    }
 }

Что я должен сделать?

  • 0
    that null ?
  • 0
    Вы должны определить «не работает» в первую очередь. Затем предоставьте минимальный рабочий пример, демонстрирующий / воспроизводящий проблему.
Показать ещё 1 комментарий
Теги:
operator-keyword
copy
variable-assignment

1 ответ

1
Лучший ответ

Вы не инициализируете k ничем, поэтому курсы [k] могут быть ссылкой в любом месте. Основные типы не инициализируются по умолчанию в c++.

  • 0
    иногда я так глуп ... спасибо!

Ещё вопросы

Сообщество Overcoder
Наверх
Меню