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

问题描述:

我试图访问我的载体,我在一个结构初始化,但是当它编译,显示此错误:如何在C++中访问结构中的向量?

mediaselec.cc: In function 'void leeVector_conjunto(ConjuntoEstudiantes&)': mediaselec.cc:23:4: error: 'ConjuntoEstudiantes' has no member named '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); 
} 
+0

编译器是对的,'ConjuntoEstudiantes'没有名为'asignaturas'的成员。结构信息确实。 – sergej

+0

你想完成什么?我不明白这段代码应该做什么。 – sergej

+0

嗨sergej,我尽我所能,翻译我的简单代码。那么现在我只想在一个向量中存储一组学生,每个人都有num_marks。问题是,我不知道我是否正在访问结构中的矢量。 – BigHelmet

如果你写

v.asignaturas(num_asignaturas); 

我想你意

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

编辑:当您编辑将您的变量名称翻译为英语和为了显示更多的代码,我告诉你要纠正的行被删除了。但这是一个必要的步骤。

+0

你是对的!谢谢!我想道歉我的英语水平低。 – BigHelmet

+0

现在我只是添加该行代码,所有工作都很好。 – BigHelmet