首页 文章

SFML访问冲突

提问于
浏览
-6

我在C和DrawBoard()函数中制作一个俄罗斯方块游戏我收到的错误是:在Tetris_C中执行0x0F52DA36(sfml-graphics-d-2.dll)的异常.exe:0xC0000005:访问违规写入位置0x013EDA88 .

如果存在此异常的处理程序,则可以安全地继续该程序 .

这是代码:

void DrawBoard() {

for (int i = 0; i < boardWidth; i++)
{
    for (int j = 0; j < boardHeight; i++)
    {
        switch (board[i][j]) {//What's wrong with this?
        case 'b':
            squares[i][j].setFillColor(sf::Color::Blue);
            break;
        case 'c':
            squares[i][j].setFillColor(sf::Color::Cyan);
            break;
        case 'y':
            squares[i][j].setFillColor(sf::Color::Yellow);
            break;
        case 'o':
            squares[i][j].setFillColor(sf::Color(255, 165, 0));//Orange
            break;
        case 'p':
            squares[i][j].setFillColor(sf::Color(150, 50, 250));//Purple
            break;
        default:
            break;
        }
    }
}

for (int x = 0; x < boardWidth; x++) {
    for (int y = 0; y < boardHeight; y++) {
        window.draw(squares[x][y]);
    }
}

}

2 回答

  • 0

    该错误只是意味着您的程序试图访问其分配的地址空间之外的内存 . 但是没有看到SSCCE就不可能知道你的错误在哪里 . 猜测是你的一个变量已超出范围,或者你试图在 squares 数组的范围之外访问 - 但同样,这只是基于不完整信息的猜测 .

  • 0
    for (int j = 0; j < boardHeight; i++)
    

    这就是让你得到分段错误的原因(我确定它是一个错字)

    你只得到你的外循环1迭代,然后内循环通过递增 i 直到它变得等于 boardwidth 来产生段错误

    代码是自我解释的,所以我确定这应该是:

    for (int j = 0; j < boardHeight; j++)
    

相关问题