C++ SDL,正在使用未初始化的变量

C++ SDL, variable is being used without being initialized

当我尝试编译这段代码时,出现错误:

The variable 'screen' is being used without being initialized.

谁能告诉我为什么?

class Board
{
    public:
    int board[BOARD_HEIGHT][BOARD_WIDTH];
    void drawboard(SDL_Surface *screen) {
        for (int i = 0; i < BOARD_HEIGHT; i++) {
            for (int j = 0; j < BOARD_WIDTH; j++) {
            if (i == 0 || i == BOARD_HEIGHT || j == BOARD_WIDTH || j == 0) {
                DrawRectangle(screen,(j*BLOCK_HW),(i*BLOCK_HW) , BLOCK_HW, BLOCK_HW, 0x000000FF, 0x000000FF);
                board[i][j] = FILLED;
            }
        }
    }
}

int main(int argc, char **argv) 
{
    SDL_Surface *screen;
    Board board;
    board.drawboard(screen);
    SDL_FreeSurface(screen);
    SDL_Quit();
    return 0;
};

这意味着您没有初始化下面的 screen 变量。

SDL_Surface *screen;

你应该使用 SDL_CreateRGBSurface.

SDL_Surface *screen = SDL_CreateRGBSurface(...);

更新:

如果这是主显示面,你应该使用SDL_CreateWindow or SDL_CreateWindowAndRenderer

示例:

SDL_Window *window;                    // Declare a pointer

SDL_Init(SDL_INIT_VIDEO);              // Initialize SDL2

// Create an application window with the following settings:
window = SDL_CreateWindow(
    "An SDL2 window",                  // window title
    SDL_WINDOWPOS_UNDEFINED,           // initial x position
    SDL_WINDOWPOS_UNDEFINED,           // initial y position
    640,                               // width, in pixels
    480,                               // height, in pixels
    SDL_WINDOW_OPENGL                  // flags - see below
);

本杰明·林德利提供。