gpt4 book ai didi

c++ - SDL_rect 没有命名类型

转载 作者:行者123 更新时间:2023-11-28 00:26:05 25 4
gpt4 key购买 nike

我一直在试用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com