Я пытаюсь сделать оператор назначения копии. Но это не сработает. В чем проблема? есть ли другой способ написать оператор присваивания копии?
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;
}
}
Что я должен сделать?
Вы не инициализируете k ничем, поэтому курсы [k] могут быть ссылкой в любом месте. Основные типы не инициализируются по умолчанию в c++.
that
null
?