[英]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.