我的宏函数有什么问题吗?

发布于 2024-11-26 20:27:30 字数 273 浏览 0 评论 0原文

我使用续行符“\”定义了一个多行宏函数,如下所示:

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

那些过往 2024-12-03 20:27:31

在 Visual Studio 编辑器中按 Ctrl+Shift+8,然后查看尾随 \ 之后出现的所有空格 - 将其删除!

Hit Ctrl+Shift+8 in Visual Studio editor and see any whitespaces appearing after the trailing \ - remove them!

糖粟与秋泊 2024-12-03 20:27:30

多语句宏的常用方法类似于:

#define SHOWMSG(msg)                                  \
do {                                                  \
    std::ostringstream os;                            \
    os << msg;                                        \
    throw CMyException(os.str(), __LINE__, __FILE__); \
} while (0)

如果没有这个,右大括号后面的分号可能会导致语法问题,例如:

if (x)
    SHOWMSG("This is a message");
else
    // whatever

按照您的代码,这将扩展为:

if (x) {
    std::ostringstream os;
    os << "This is a message";
    throw CMyException(os.str(), __LINE__, __FILE__);
}
;    // on separate line to emphasize that this is separate statement following
     // the block for the if statement.
else
    // whatever

在这种情况下,分号将形成if 语句中的块后面有一个 null 语句,并且 else 没有 if 可以匹配。

The usual method for a multi-statement macro is something like:

#define SHOWMSG(msg)                                  \
do {                                                  \
    std::ostringstream os;                            \
    os << msg;                                        \
    throw CMyException(os.str(), __LINE__, __FILE__); \
} while (0)

Without that, the semicolon following the closing brace can cause syntactic problems, such as:

if (x)
    SHOWMSG("This is a message");
else
    // whatever

With your code as it was, this would expand to:

if (x) {
    std::ostringstream os;
    os << "This is a message";
    throw CMyException(os.str(), __LINE__, __FILE__);
}
;    // on separate line to emphasize that this is separate statement following
     // the block for the if statement.
else
    // whatever

In this case, the semicolon would form a null statement following the block in the if statement, and the else wouldn't have an if to match up with.

轮廓§ 2024-12-03 20:27:30

反斜杠必须是该行的最后一个字符才能继续该行。

有些反斜杠后面有空格。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文