lundi 25 juillet 2016

Segmentation fault in push_back for a 2d vector

I'm learning c++ and I have a problem with a segmentation fault. In my project I want to read from a File into a 2d Vector of char. The Vector is std::vector<std::vector<char>> gamearea;

void Structure::readFile(const std::string filename) 
{ 
    std::ifstream file(filename.c_str()); 
    if (!file.is_open()) 
    { 
       std::cerr << "Error opening file: " << filename << std::endl;    
       exit(1); 
    } 
    std::string line; 
    int i = 0; 
    while (true) 
    { 
       std::getline(file, line); 
       if (file.eof()) 
       { 
          break; 
       } 
       for (size_t j = 0; j< line.length(); j++) 
       { 
           gamearea[i].push_back(line[j]); 
       } 
       i++; 
    } 
}

This is my read file function and the debugger (I use gdb) says by push_back is a segmentation fault.

Can someone help me? I can't find the problem.

Aucun commentaire:

Enregistrer un commentaire