I have a set of Students and an iterator which finds a specific student which I then need to change. The problem is, when I go to change the object the pointer points to, it says the object is const. I'm not sure why this is, as I do not think I ever explicitly make the objects constant. I am relatively new to C++, so I may be doing something to make the Student objects const accidentally.
Here is the main funcion
set<Student> students;
ifstream file(*somefilename*);
while (!file.is_open())
{
cout << filename << endl;
cout << "Could not open file. Enter new filename: ";
cin >> filename;
file.open(filename);
}
while (!file.eof()) {
string temp = "";
string name;
int regNo;
if (file.eof())break;
for (int i = 0; i < 3; i++) {
if (i == 0)
file >> regNo;
else {
file >> temp;
name += temp;
}
}
cout << "For loop done" << endl;
students.insert(Student(name, regNo));
}
file.close();
file.open("ex1/marks.txt");
while(!file.eof()){
int regNo;
string module;
int mark;
file >> regNo;
Student tempStud("",regNo);
file >> module;
file >> mark;
set<Student>::iterator it = students.find(tempStud);
if (it != students.end()) {
**it->addMark(module, mark);**//here's the problem code
}
}
file.close();
for (set<Student>::iterator it = students.begin(); it != students.end(); it++)
cout << *it << endl;
cin.get();}
And here is the header file for the Student class
public:
Student(const string &name, int regNo);
int getRegNo() const;
void addMark(string& module, float mark);
float getMark(const string &module) const;
float getMin() const;
float getMax() const;
float getAvg() const;
bool operator <(const Student& s2) const;
bool operator >(const Student& s2);
bool operator ==(const Student& s2);
private:
int regNo;
map<string, float> marks; // keys are modules, values are marks in range 0.0 to 100.0
friend ostream& operator<<(ostream &str, const Student &s);