c++ - SDL_rect 没有命名类型

标签 c++ sdl-2

我一直在试用 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 Works\SDL Stretching\TestingSDL.cpp|12|error: 'stretchRect' does not name a type|
D:\My Works\SDL Stretching\TestingSDL.cpp|13|error: 'stretchRect' does not name a type|
D:\My Works\SDL Stretching\TestingSDL.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;
    
        ...
    

关于c++ - SDL_rect 没有命名类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24990793/

相关文章:

c++ - SDL2 : LNK1561: entry point must be defined

c++ - SDL tilemap 渲染很慢

c++ - 为什么在 "release "模式下生成的可执行文件给出了错误的结果?

sdl-2 - SDL2 : Is SDL_GetMouseState() thread-safe?

c++ - SDL_TTF 渲染错误的地方

c++ - 渲染时如何使SDL2 0,0定位窗口的中心?

c++ - 无法从指向基类的指针转换为指向派生类的指针

c++ - 将自定义小部件显示到布局中

c++ - SDL_GAMECONTROLLERCONFIG 与 SDL_GameControllerAddMappingsFromFile

c++ - CUDA 异常行为访问 vector