c++ - Vector of vector : Segmentation Fault -


i have segmentation fault error @ runtime, here code indication of line error occurs :

edit : variable scenario attribute of class probleme, code posted code of constructor

edit : the error happens @ runtime

class scenario { public:     int id;     std::vector<std::vector<int> > demandes;      scenario() {}     void setid(int i) { id=i; }     void setdemande( std::vector<int>  v) { demandes.push_back(v); }     int getid() const { return id; } };  scenario.resize(nb_scenario); (int = 0; < nb_scenario; ++i) {     scenario[i].setid(i); }     int s;      for(int i=0; i<nb_periode ; i++) {         fichier >> s ;         for(int j=0;j<nb_produit;j++){             fichier >> s ;                 for(int k=0;k<nb_scenario;k++){                     fichier >> s;                     scenario[k].demandes[j][i]= s; //error here                 }         }                     } 

you never add demandes vector. calling operator[] on empty vector (the index out of bounds) undefined behavior.

to fix add elements demandes using push_back or resize. resize inner vectors @ same time can demandes.resize(n, std::vector<int>(m)). consider using at instead of operator[], since throw exception on out of bounds access instead of causing undefined behavior.


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -