我尝试执行http://www.cprogramming.com/tutorial/game_programming/same_game_part1_p2.html中描述的游戏。虽然它最初运行良好,但从运行的某一时刻起,在运行时崩溃,而构建并不表示出现任何错误。这个问题似乎是“处理异常” - 上线未处理的异常
return m_arrColors[m_arrBoard[row][col]];
“访问冲突读取位置”的功能
COLORREF CSameGameBoard::GetBoardSpace(int row, int col)
{
// Check the bounds of the array
if(row < 0 || row >= m_nRows || col < 0 || col >= m_nColumns)
return m_arrColors[0];
return m_arrColors[m_arrBoard[row][col]];
}
任何可能的原因是什么?
更新:
程序崩溃尝试访问
m_arrColors[m_arrBoard[0][0]];
m_arrColors和m_arrBoard首次由构造函数定义如下:我加入了:
CSameGameBoard::CSameGameBoard(void)
:m_arrBoard(NULL),
m_nColumns(15), m_nRows(15),
m_nHeight(35), m_nWidth(35)
{
m_arrColors[0] = RGB( 0, 0, 0);
m_arrColors[1] = RGB(255, 0, 0);
m_arrColors[2] = RGB(255,255, 64);
m_arrColors[3] = RGB( 0, 0,255);
}
UPDATE2命令SetupBoard();在构造函数的正文中,它起作用了。然而,它不是由教程http://www.cprogramming.com/tutorial/game_programming/same_game_part1_p2.html提出的,并且最初在我的程序中正常工作,但没有它。
什么是'm_arrColors'和'm_arrBoard'?碰撞发生时,“row”和“col”是什么?很确定你只是出界了。 – tenfour 2011-12-19 08:07:47
您是否检查过“m_arrBoard [row] [col]'是否有效?你有没有试过在调试器中运行它? – 2011-12-19 08:10:48