我遇到了一个宏问题,我不知道为什么。
这是宏:
#define WAIT(condition, max_time) \
do { \
int int_loop_wait=0; \
while(1) \
{ \
if(condition) { break; } \
sleep(1); \
if(int_loop_wait>=max_time) { break; } \
int_loop_wait++; \
} \
} while(0) \
我得到了错误
"expected a declaration" line " if(condition) { break; } "
有人理解这个错误吗?
最佳答案
问题是反斜杠后跟一个空格一起被识别为转义序列,实际上取消了反斜杠。 Visual C++ 10 甚至在那里发出 error C2017: illegal escape sequence
。
代码片段中的某些行(例如带有 while(1)
的行)在反斜杠后包含一个或多个空格。一旦反斜杠被视为转义序列并被编译器删除,宏定义就会在该行被截断,其余代码将被编译,就好像它不属于宏定义一样。
#define WAIT(condition, max_time) \
do { \
int int_loop_wait=0; \
while(1) \ <<<<<WHITESPACES
{ \<<<this line doesn't belong to macro
if(condition) { break; } \<<<and neither does this
sleep(1); \
if(int_loop_wait>=max_time) { break; } \
int_loop_wait++; \
} \
} while(0) \
关于c - 这个宏有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15635588/