C++对象实例无法读取内存

use*_*680 1 c++ pointers reference object c++11

我需要一些关于c ++和objects\references\pointers的帮助.我正在写一个简单的国际象棋程序,它有几个类.特别是我有以下两个类Board和Cell的问题:

该板具有单元矢量矢量(基本上是矩阵).每个单元格都有一个指向占用它的当前片段的指针.

class Board
{
public:
    Board();
    void drawBoard();
    bool makeMove(int startRow, int startCol, int destRow, int destCol);
private:
    vector< vector<Cell> > _board;
    void initBoard();
    void initPieces();
};

Board::Board()
{
    initBoard();
}

void Board::initBoard()
{

    for (int i = 0; i < BOARD_SIZE; i++)
    {
        vector<Cell> row; //create empty row
        for (int j = 0; j < BOARD_SIZE; j++)
        {
            row.push_back((Cell(i, j)));
        }
        _board.push_back(row);
    }

    initPieces();
}

void Board::initPieces()
{
    //Set Pawns
    for (int i = 0; i < BOARD_SIZE; i++)
    {
        _board[1][i].setOccupying(new Pawn(White));
        _board[6][i].setOccupying(new Pawn(Black));
    }
}

void Board::drawBoard()
{

    drawLettersCoord();
    for (int i = BOARD_SIZE-1; i >= 0; i--)
    {
        std::cout << i + 1 << " ";
        for (int j = 0; j < BOARD_SIZE; j++)
        {       
            _board[i][j].draw();
        }
        std::cout << " " << i + 1 << std::endl;
    }
    std::cout << "\n";

}

bool Board::makeMove(int startRow, int startCol, int destRow, int destCol)
{
    _board[startRow][startCol].getOccupyingPiece()->isLegalMove(
        startRow, startCol, destRow, destCol);
    return true;
}
Run Code Online (Sandbox Code Playgroud)

这是Cell类:

class Cell
{
public:
    Cell();
    Cell(int row, int col);
    Cell(int row, int col, ChessPiece * occupying);
    void draw();
    ChessPiece * getOccupyingPiece();
    void setOccupying(ChessPiece *occupying);
private:
    int _row;
    int _col;
    bool _occupied;
    ChessPiece * _pieceOccupying;
};

Cell::Cell()
    :Cell(0, 0)
{
    setColour();
}

Cell::Cell(int row, int col)
    : _row(row), _col(col), _occupied(false)
{
    setColour();
}

Cell::Cell(int row, int col, ChessPiece * occupying)
    : _row(row), _col(col), _pieceOccupying(occupying), _occupied(true)
{
    setColour();
}

void Cell::setOccupying(ChessPiece *occupying)
{
    _occupied = true;
    _pieceOccupying = occupying;
}
void Cell::draw()
{

    int foregroundText;
    if (!_occupied)
    {
        foregroundText = 37;
    }
    else
    {
        foregroundText = _pieceOccupying->getPlayer();
    }
    cout << "\33[" << foregroundText << ";" << _colour << "m"
         << (_occupied ? _pieceOccupying->getPieceCode(): " ") << "\33[0m";
}

ChessPiece * Cell::getOccupyingPiece()
{
    return _pieceOccupying;
}
Run Code Online (Sandbox Code Playgroud)

当我运行游戏时,我通过调用创建并绘制一个新的Board

Board _board;
_board.drawBoard();
Run Code Online (Sandbox Code Playgroud)

图纸似乎没有任何问题.

但是,当我打电话的时候

_board.makeMove(1,0,2,0);
Run Code Online (Sandbox Code Playgroud)

检查pawn是否可以进行此类移动,我收到内存错误.在调试时,我看到被调用的Cell对象中有垃圾,而不是实际数据.当我试图查看指向占用件的指针时,它显示"无法读取内存",因此当我调用占用件的isLegalMove函数时,它会崩溃.

我似乎无法找出问题所在.我不明白为什么向量中的单元格会在其中有垃圾,它在类中定义(没有新的)因此根据我的理解它应该是可用的,只要当前的板实例是活的.

谁能解释我做错了什么?

Rei*_*ica 5

  1. 并不是所有的建设者CellINITIALISE _occupied_pieceOccupying,这意味着这些可以在一些垃圾值Cell对象.

  2. 此外,您无条件makeMove取消引用getOccupyingPiece()- 它不检查null.

  3. 最后,你的棋盘上只有棋子,这意味着(0, 0)不包含一块 - 而且由于1.,Cell对象中包含一个垃圾值_pieceOccupying,所以你访问随机内存并崩溃.