向量的访问元素

我有一个班级学生:

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;
}

和班级组:

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;
        }
    }
};

在小组中,我正试图使<< <

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;
    }

但是我有这个错误:

error C2676: binary '[': 'const Group' does not define this operator or a conversion to a type acceptable to the predefined operator

所以,我搜索了[]个重载了vector的运算符,但没有找到对我有用的东西。我也尝试过复制构造函数,但并没有帮助我。如何使用group [i] .getInfo()?或者,也许有其他方法可以访问它。因此,group [i]必须是Student对象。

0
投票

似乎您将类型为groupGroup与它的成员混淆,该成员也被混淆为group

要么提供Group::operator[],要么将operator<<更改为

friend ostream& operator<<(ostream& out, const Group& group) { // overloaded operator of output
        auto& vect = group.group;
        out << "\nThere are " << vect.size() << " students in the group.\n";
        for (int i = 0; i < vect.size(); i++)
        {
            out << "Student # " << i + 1 << ":\n";
            out << vect[i].getInfo();
        }

        return out;
    }