繁体   English   中英

如何在 C++ 中访问结构中的向量?

[英]How to access a vector in a struct in C++?

我试图访问我在结构中初始化的向量,但是当它编译时,打印此错误:

mediaselec.cc:在函数“void leeVector_conjunto(ConjuntoEstudiantes&)”中:mediaselec.cc:23:4:错误:“ConjuntoEstudiantes”没有名为“asignaturas”的成员 v.asignaturas(num_asignaturas);

#include <vector>
#include <iostream>
using namespace std;

struct Info{
    int id_student;       //id of a student
    vector<double> marks; //Contains all the marks of one student
};

//typedef vector<int> Subconjuntos;
typedef vector<Info> StudentGroup;

/*Read a vector of n student group*/
void enter_group(StudentGroup& v, Subconjuntos& s) {
    //Size of the StudentGroup
    int n;
    cin >> n;
    v = StudentGroup (n);
    //Num. marks of one student
    int num_marks;
    cin >> num_marks;

    //Ignore this part.
    /*
    //Numero de subconjuntos
    int s_subconjuntos;
    cin >> s_subconjuntos;
    s = Subconjuntos (s_subconjuntos);
    for (int i = 0; i < n; ++i) {
        cin >> s[i]; 
    }
    */

    //Read all the students with all the marks and store it in v.
    for (int i = 0; i < n; ++i) {
        cin >> v[i].id_student;
        for (int j = 0; j < num_marks; ++j) {
            cin >> v[i].marks[j];
        }
    }
}


int main() {
    StudentGroup v;
    //Subconjuntos s;
    enter_group(v,s);
}

你写的地方

v.asignaturas(num_asignaturas);

我想你是故意的

v[n].asignaturas.resize(num_asignaturas);

编辑:当您编辑以将变量名称翻译成英文并显示更多代码时,我告诉您更正的行已被删除。 但这是必要的步骤。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM