簡體   English   中英

類定義中的bad_alloc錯誤

[英]bad_alloc error in class definition

這是我正在上學的一些代碼。 當我運行它時,我收到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;
}

電路板構造函數-第二個for()函數,您有錯誤的終止條件。 應該有“ j <cols_”而不是“ i <cols_”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM