I have a class Student:
class Student {
private:
unsigned int id;
string name;
vector<int> grades;
public:
Student(unsigned int id, string name, vector<int> grades);;
virtual ~Student() {}
unsigned int getId() { return this->id; }
string getName() { return this->name; }
int getGradesAmount() { return this->grades.size(); }
vector<int> getGrades() { return this->grades; }
int getGrade(int i) { return this->grades[i]; }
unsigned int getCoef()
{
unsigned int coef = 1;
for (int i = 0; i < this->grades.size(); i++) { coef *= this->grades[i]; }
return coef;
}
int getNameCoef() { return this->getName().size() % 2; }
ostringstream getInfo()
{
ostringstream info;
info << "ID: " << getId() << ".\n";
info << "Name: " << getName() << ".\n";
info << "Amount of grades: " << getGradesAmount() << ".\n";
info << "Grades:";
for (int i = 0; i < getGradesAmount(); i++)
info << " " << getGrade(i);
info << "\nProduct of grades: " << getCoef() << ".\n";
info << "Is surname has odd number of symbols (0 = no / 1 = yes): " << getNameCoef() << ".\n";
return info;
}
};
Student::Student(unsigned int id, string name, vector<int> grades)
{
this->id = id; this->name = name; this->grades = grades;
}
And a class Group:
class Group : public Student {
protected:
int size = 0;
vector<Student> group;
public:
Group() : Student(getId(), getName(), getGrades()) {}
void addStudent(Student student)
{
if (student.getNameCoef() == 1)
{
if (this->group.size() > 0)
{
for (int i = 0; i < this->group.size(); i++)
{
if (student.getCoef() > group[i].getCoef())
{
this->group.insert(this->group.begin() + i, student);
this->size = this->size + 1;
return;
}
}
}
cout << "\nAdded to start";
this->group.push_back(student);
this->size = this->size + 1;
}
}
};
In Group I'm trying to overload << to make a cout << group. So, I have added this into a Group:
friend ostream& operator<<(ostream& out, const Group& group) { // overloaded operator of output
out << "\nThere are " << group.size << " students in the group.\n";
for (int i = 0; i < group.size; i++)
{
out << "Student # " << i + 1 << ":\n";
out << group[i].getInfo();
}
return out;
}
But I have this error:
error C2676: binary '[': 'const Group' does not define this operator or a conversion to a type acceptable to the predefined operator
So, I googled for any [] overloaded operators for vector but didn't find anything that work for me. I also tried copying constructor, but it didn't helped me. How to use group[i].getInfo() ? Or maybe there are some oter ways to access this. So, group[i] must be Student object.