我的宏函数有什么问题吗?
我使用续行符“\”定义了一个多行宏函数,如下所示:
#define SHOWMSG( msg ) \
{ \
std::ostringstream os; \
os << msg; \
throw CMyException( os.str(), __LINE__, __FILE__ ); \
}
但它无法通过编译。顺便说一句,我使用的是 VS2008 编译器。请问我的上述宏函数有什么问题吗?
I defined a multi-line macro function using line continuation character "\" like the following:
#define SHOWMSG( msg ) \
{ \
std::ostringstream os; \
os << msg; \
throw CMyException( os.str(), __LINE__, __FILE__ ); \
}
But it could not pass compilation. BTW, I'm using VS2008 compiler. Would you please tell what's wrong with my aforesaid macro function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 Visual Studio 编辑器中按
Ctrl+Shift+8
,然后查看尾随\
之后出现的所有空格 - 将其删除!Hit
Ctrl+Shift+8
in Visual Studio editor and see any whitespaces appearing after the trailing\
- remove them!多语句宏的常用方法类似于:
如果没有这个,右大括号后面的分号可能会导致语法问题,例如:
按照您的代码,这将扩展为:
在这种情况下,分号将形成
if
语句中的块后面有一个 null 语句,并且else
没有if
可以匹配。The usual method for a multi-statement macro is something like:
Without that, the semicolon following the closing brace can cause syntactic problems, such as:
With your code as it was, this would expand to:
In this case, the semicolon would form a null statement following the block in the
if
statement, and theelse
wouldn't have anif
to match up with.反斜杠必须是该行的最后一个字符才能继续该行。
有些反斜杠后面有空格。
The backslash must be the last character on the line in order for the line to be continued.
Some of your backslashes have spaces after them.