2015-09-26 103 views
-2

我試圖訪問我的載體,我在一個結構初始化,但是當它編譯,顯示此錯誤:如何在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

回答

0

如果你寫

v.asignaturas(num_asignaturas); 

我想你意

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

編輯:當您編輯將您的變量名稱翻譯爲英語和爲了顯示更多的代碼,我告訴你要糾正的行被刪除了。但這是一個必要的步驟。

+0

你是對的!謝謝!我想道歉我的英語水平低。 – BigHelmet

+0

現在我只是添加該行代碼,所有工作都很好。 – BigHelmet