调试错误
问题描述:
可以请人给我解释一下我究竟做错了什么,我收到文件的错误意外结束,也缺少函数头,在此先感谢调试错误
static char debug[256];
#define DBGPRINT(...) {sprintf_s(debug, 256, __VA_ARGS__); OutputDebugStringA(debug);}
#define CHECK_READ(status, str) while(0){ \
if(0 == status){ \
DBGPRINT("Message %s\n", str); \
return 0; \
} \
}
int main(){
char* str = "hello world";
status = 0;
CHECK_READ(status, str);
return 0;
}
错误:
Error line 7 error C2447: '{' : missing function header (old-style formal list?)
Error line 11 error C2447: '{' : missing function header (old-style formal list?)
Error line 15 error C2017: illegal escape sequence
Error line 19 fatal error C1004: unexpected end-of-file found
答
好的这里是真正的答案。
我复制并从上面的代码粘贴,并且您已经在宏定义行
if(0 == status){ \ WHITESPACE HERE
对于blackslash作为续行字符操作的一台后的空白,它必须是最后一个字符该行,之后没有空格。现在谁知道这是否是你的实际问题,但与空白我得到了同样的错误,没有它,我没有。
+0
最好的,非常感谢你 – yeap
答
在宏定义结束时您还有一个额外的\
。所以,你的int main(){
行实际上是宏CHECK_READ
的一部分:)
编辑:
无尾\
的变体编译以及对ideone:http://ideone.com/pddx0。我声明status
,因为它没有在你的代码中声明。 (我注释掉了OutputDebugStringA
并用snprintf
代替sprintf_s
,因为它们都是微软专用的,不会在gcc上编译。)
尽可能不要在C++中使用宏和原始char *字符串。 –
@dark_charlie:在特定情况下,用同样紧凑的东西来替换宏将会非常复杂。但是当然,char *字符串是邪恶的。 – Vlad
@Vlad:你的宏是NOOP。它可以很好地替换为空行。 – Mat