Hello and thanks for taking the time to look at this.
The following code is part of my class definition and implementation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Definition
Private:
int *grades;
int numGrades;
Public:
Student1 &addGrade(int ) const;
// Implementation
Student1 &Student1::addGrade(int g ) const{
grades[numGrades] = g;
numGrades++;
return *this;
}
My question is why am I getting an error for incrementing private data member numGrades (Line 12) but no errors for assigning a value to private data member *grades (Line 11) ?
line 11 doesn't change 'grades'... it changes what grades points to. Even if grades is constant (which is the case in this const function), it doesn't mean what it points to is constant.
Student1::grades is never made to point to valid memory, so line 27 will always fail.
Student1::~Student1() is leaking the memory for Student1::name.
Student1::addGrade() is returning a copy of *this, which will cause trouble because you haven't defined a copy constructor and the class owns allocated pointers.