SDL_rect不命名类型

时间:2014-07-28 07:58:07

标签: 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 个答案:

答案 0 :(得分:3)

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

有两种解决方案:

  1. 在声明中初始化,如

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

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