C++中的rmask不是一种类型

3

我从sdl2文档中获取了以下代码:

//Color declartions for later
Uint32 rmask, gmask, bmask, amask;

#if SDL_BYTEORDER == SDL_BIG_ENDIAN
    rmask = 0xff000000;
    gmask = 0x00ff0000;
    bmask = 0x0000ff00;
    amask = 0x000000ff;
#else
    rmask = 0x000000ff;
    gmask = 0x0000ff00;
    bmask = 0x00ff0000;
    amask = 0xff000000;
#endif

Codeblocks告诉我条件为真,但在编译时,它告诉我rmask不是类型的名称。错误从else语句的第一行开始标记。首先,我该如何避免这种情况?其次,我是否需要if语句?
完整的错误日志如下:
||=== Build: Debug in hayfysh (compiler: GNU GCC Compiler) ===|
/home/andrew/hayfysh/main.cpp|57|error: ‘rmask’ does not name a type|
/home/andrew/hayfysh/main.cpp|58|error: ‘gmask’ does not name a type|
/home/andrew/hayfysh/main.cpp|59|error: ‘bmask’ does not name a type|
/home/andrew/hayfysh/main.cpp|60|error: ‘amask’ does not name a type|
/home/andrew/hayfysh/main.cpp||In function ‘int newWindow(int, int, bool, const char*)’:|
/home/andrew/hayfysh/main.cpp|90|error: cannot convert ‘const char*’ to ‘FILE* {aka _IO_FILE*}’ for argument ‘1’ to ‘int fprintf(FILE*, const char*, ...)’|
||=== Build failed: 5 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

通过将fprintf更改为printf已经修复了第五个错误


4
你是否已经适当地为Uint32进行了类型定义?否则你应该使用uint32_t - davidhigh
uint32_t 不改变任何东西 - Andrew Robinson
你是否包含了SDL头文件?我很确定SDL定义了Uint32。编辑:SDL在这里定义了Uint32:http://hg.libsdl.org/SDL/file/8da3e4d25202/include/SDL_stdinc.h,而且`SDL_stdinc.h`被包含在`SDL.h`中。 - simon
我可能还不需要SDL_image,但是嘿,很快我就会用到它了。 - Andrew Robinson
如果你只尝试使用 int 会发生什么?你需要包含 stdint.h / cstdint 来使用 uint32_t - simon
显示剩余5条评论
1个回答

3
假设这段代码就是出现问题的地方(即它没有从函数中间提取出来),问题在于全局作用域不允许赋值语句。将其更改为初始化变量(如果应该标记为const?):
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
    Uint32 rmask = 0xff000000;
    Uint32 gmask = 0x00ff0000;
    Uint32 bmask = 0x0000ff00;
    Uint32 amask = 0x000000ff;
#else
    Uint32 rmask = 0x000000ff;
    Uint32 gmask = 0x0000ff00;
    Uint32 bmask = 0x00ff0000;
    Uint32 amask = 0xff000000;
#endif

非常感谢。现在我感到很愚蠢,因为解决方案如此简单 :/ - Andrew Robinson
说句实话,在SO上发布的代码片段中,似乎有太多的变量被创建然后再赋值,而不是初始化。不知道为什么这成了一种趋势... - Pete Becker

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接