c++ - 访问 vector 的元素

标签 c++ vector

我有一个类(class)学生:

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

在组中,我试图重载 << 以创建 cout << 组。 因此,我已将其添加到一个组中:

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

因此,我在 google 上搜索了 vector 的任何 [] 重载运算符,但没有找到任何适合我的内容。我也尝试过复制构造函数,但它对我没有帮助。 如何使用 group[i].getInfo() ?或者也许还有其他一些方法可以访问它。因此,group[i] 必须是 Student 对象。

最佳答案

您似乎混淆了 group其类型为 Group及其成员,它也被称为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;
    }

关于c++ - 访问 vector 的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60937225/

相关文章:

c++ - 如何使用Boost获取2D点的凸包面积?

c++ - 在 C++ 中从 Mat 中提取 Y、Cb、Cr 并创建仅包含 Y 值的 Mat

c++ - 是否可以用 C++ 创建网页?

C++14:如何按模板参数对可变输入进行分组?

c++ - 如何用元素位数之和替换vector中的所有元素?

C++ Vector 下标超出范围(但事实并非如此)

c# - 如何在没有延迟的情况下在 Windows 中重新定位图形?

scala - 通用微风向量法

c++ - 初始化类内 vector vector 的两个维度

c++ - struct 排序 vector 的 lower 和 upper_bound