increment of data-member in read-only structure

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) ?

Thanks very much for any help!
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.
Thanks Disch,

So, *grades is a constant pointer to non-constant data?

Given the following public functions:

1
2
3
4
5
6
public:
		Student1(const char *);
		~Student1();
		void displayGrades() const;
		Student1 &addGrade(int ) const;
		static int getNumStudents();


Is there a way to change numGrades each time I call the addGrade member function??
Okay, please if someone can help....this is the class definition which I have.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Given
class Student1{

	private:
		int *grades;
		char *name;
		int numGrades;
		int idNum;
		static int numStudents;

	public:
		Student1(const char *);
		~Student1();
		void displayGrades() const;
		Student1 addGrade(int );
		static int getNumStudents();

};


And, this is my implementation of it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include"student1.h"
#include<cstring>
#include<iostream>
using std::cout;
using std::endl;

int Student1::numStudents = 0;

Student1::Student1(const char *n){ 
	name = new char[strlen(n)];
	strcpy(name, n);
	idNum = numStudents;
	numStudents++;
	numGrades = 0;
}

Student1::~Student1(){}

void Student1::displayGrades() const{
	cout << "\nDisplaying Grades for " << name << " id " << idNum + 1 << ":";
	for(int i = 0; i < numGrades; i++)
		cout << "\nGrade " << i+1 << "=  " << grades[i];
	cout << endl;
}

Student1 Student1::addGrade(int g ){
	grades[numGrades] = g;
	numGrades++;
	return *this;
}
	
int Student1::getNumStudents() { return numStudents; }


My main problem is incrementing numGrades, which is giving me an error. Or, is my logic for implementing the given wrong?

Thanks very much for any help!
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.
also, you're corrupting the heap. You're not allocating enough space for 'name'. You need to allocate an extra 1 for the null terminator.

Of course.. all of these headaches would go away if you used std::string instead of char arrays.
Topic archived. No new replies allowed.