在 Objective-C 中使用宏时出现错误
我是 Objective-C 的新手。我正在尝试使用宏的示例程序并收到错误。
#import <Foundation/Foundation.h>
#define HELLO_WORLD @"Hello World"
#define a(x,y) ((x)+(y))
#define PRINTMAC(x,y)\
NSLog(@"%d",a((x),(y));\
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// insert code here...
//NSLog(@"%d",add(10,20));
PRINTMAC(13,72); //error:
[pool drain];
return 0;
} //error:
错误:预期为“;”在“}”标记之前
I am new to Objective-C. I was trying out a sample program using macros and getting errors.
#import <Foundation/Foundation.h>
#define HELLO_WORLD @"Hello World"
#define a(x,y) ((x)+(y))
#define PRINTMAC(x,y)\
NSLog(@"%d",a((x),(y));\
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// insert code here...
//NSLog(@"%d",add(10,20));
PRINTMAC(13,72); //error:
[pool drain];
return 0;
} //error:
Error: expected ';' before '}' token
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您似乎在
NSLog
行(第 8 行)上缺少)
。此外,我不确定您是否需要该行的最后一个
\
,因为宏没有被带到第三行。最后,我认为您也不需要该行上的
;
,因为当您调用第 15 行上的宏时,它与分号结合会导致空语句(不应该是有害的) , 尽管)。You appear to be missing a
)
on theNSLog
line (line 8).Additionally, I'm not sure you need the final
\
on that line, as the macro is not being carried on to a third line.Finally, I don't think you need the
;
on that line either as it, combined with the semi-colon when you invoke the macro on line 15 results in an empty statement (shouldn't be harmful, though).