SDL_rect没有命名类型

SDL_rect does not name a type

本文关键字:类型 rect SDL      更新时间:2023-10-16

我一直在尝试SDL 2,并得到SDL_Rect does not name a type错误…代码:

#include <stdio.h>
#include <SDL.h>
using namespace std;

SDL_Window* gWindow = NULL;
SDL_Surface* gWSurface = NULL;
SDL_Surface* gFinalImage = NULL;
int screenW = 640;
int screenH = 480;
SDL_Rect stretchRect;
stretchRect.x = stretchRect.y = 0;
stretchRect.w = screenW;
stretchRect.h = screenH;

错误日志:

||=== Build: Debug in SDLEvents (compiler: GNU GCC Compiler) ===|
D:My WorksSDL StretchingTestingSDL.cpp|12|error: 'stretchRect' does not name a type|
D:My WorksSDL StretchingTestingSDL.cpp|13|error: 'stretchRect' does not name a type|
D:My WorksSDL StretchingTestingSDL.cpp|14|error: 'stretchRect' does not name a type|

我已经在网上检查了另一个问题,但到目前为止还没能解决这个问题....谢谢大家

如果我正确理解您显示的代码,您尝试在全局范围内初始化矩形,在任何函数之外。你这样做是不可能的,你不能在函数之外有赋值语句。

有两个解决方案:

  1. 初始化在声明中,如

    SDL_Rect stretchRect = { values... };
    
  2. 在全局作用域中声明变量,并在函数中初始化:

    SDL_Rect stretchRect;
    ...
    int main()
    {
        stretchRect.x = stretchRect.y = 0;
        stretchRect.w = screenW;
        stretchRect.h = screenH;
        ...