2015-11-18 3 views
0

Это код, над которым я работаю в школе. Когда я запускаю его, я получаю ошибку bad_alloc. Скорее всего, где-то в определениях классов, но я ничего не пробовал. Может ли кто-нибудь сказать мне, где происходит плохое распределение?ошибка bad_alloc в определении класса

#include <iostream> 
#include <vector> 

using namespace std; 

enum Dir 
{ 
    LEFT = 1, 
    RIGHT = 1 << 1, 
    UP = 1 << 2, 
    DOWN = 1 << 3, 
}; 

class Cell 
{ 
    friend class Board; 
    int row_, col_; 
    bool visited_; 
    unsigned int walls_; 
    Cell* up_; 
    Cell* down_; 
    Cell* left_; 
    Cell* right_; 

    Cell& set_up(Cell *up) 
    { 
     up_ = up; 
     return *this; 
    } 

    Cell& set_down(Cell *down) 
    { 
     down_ = down; 
     return *this; 
    } 

    Cell& set_left(Cell *left) 
    { 
     left_ = left; 
     return *this; 
    } 

    Cell& set_right(Cell *right) 
    { 
     right_ = right; 
     return *this; 
    } 

    public: 
     Cell(int row = -1, int col = -1) 
     :row_(row), col_(col), visited_(false), walls_(LEFT | RIGHT | UP | DOWN){} 

     bool visited() const 
     { 
      return visited_; 
     } 

     bool hasWall(Dir d) const 
     { 
      return walls_ & d; 
     } 

     void drill(Dir d) 
     { 
      walls_ &= ~d;   
     } 

     void draw(int step = 20) const 
     { 
      cout << col_*step << " " << row_*step << " " << "moveto" << endl; 
      cout << step << " " << 0 << " " << (hasWall(DOWN)?"rlineto":"rmoveto") << endl; 
      cout << 0 << " " << step << (hasWall(RIGHT)?"rlineto":"rmoveto") << endl; 
      cout << -step << " " << 0 << " " << (hasWall(UP)?"rlineto":"rmoveto") << endl; 
      cout << 0 << " " << -step << (hasWall(DOWN)?"rlineto":"rmoveto") << endl; 
     } 

     Cell* up() const 
     { 
      return up_; 
     } 

     Cell* down() const 
     { 
      return down_; 
     } 

     Cell* left() const 
     { 
      return left_; 
     } 

     Cell* right() const 
     { 
      return right_; 
     } 

}; 

class Board 
{ 
    friend class Cell; 
    int rows_, cols_; 
    vector<Cell> cells_; 
    public: 

     Board(int rows, int cols) 
     : rows_(rows), cols_(cols) 
     { 
      for(int i = 0; i < rows_; i++) 
       for (int j = 0; i < cols_; j++) 
        cells_.push_back(Cell(i, j)); 

      for (int i = 0; i < rows_ ; i++) 
       for(int j = 0; j < cols_; j++) 
       { 
        Cell& c = at(i, j); 
        if(i < rows_ - 1) 
         c.set_up(&at(i + 1 , j)); 
        if(i > 0) 
         c.set_down(&at(i - 1 , j)); 
        if(j > 0) 
         c.set_left(&at(i , j - 1)); 
        if(j < cols_ - 1) 
         c.set_right(&at(i, j + 1)); 
       } 
     } 

     Cell& at(int i, int j) 
     { 
      int index = i*cols_ + j; 
      return cells_[index]; 
     } 

     const Cell& at(int i, int j) const 
     { 
      int index = i*cols_ + j; 
      return cells_[index]; 
     } 

     void draw() const 
     { 
      cout << "newpath" << endl; 
      for (vector<Cell>::const_iterator it = cells_.begin(); it != cells_.end(); ++it) 
      { 
       (*it).Cell::draw(); 
      } 
      cout << "stroke" << endl; 
      cout << "showpage" << endl; 
     } 
}; 

int main() 
{ 
    Board b(5, 5); 

    Cell& c = b.at(2, 2); 
    c.drill(UP); 
    Cell* up = c.up(); 
    up->drill(DOWN); 
    b.draw(); 
    return 0; 
} 
+1

Запустите программу в отладчике и пройдите по строке за строкой, чтобы найти точный источник, в котором выбрано исключение. –

+0

Как это сделать в g ++? – Reaper9806

+2

Есть gdb, идущий с вашей toolchain. Если вы используете IDE, например Eclipse CDT или CodeBlocks, он уже интегрирован. –

ответ

1

Конструктор платы - второй для() у вас неправильное условие завершения. Должно быть «j < cols_» вместо «i < cols_».

+0

Спасибо, сейчас работает! :) – Reaper9806

Смежные вопросы