C++ 为什么MSVC在编译宏时发脾气+;都是关于禅宗的吗?

C++ 为什么MSVC在编译宏时发脾气+;都是关于禅宗的吗?,c++,c,visual-c++,C++,C,Visual C++,在MSVC 9.0下,这将失败。在g++下编译。如果我们取出宏,则编译非宏版本76-79。有什么想法吗 03: #include <iostream> 04: #include <sstream> 67: #define MAKESTRING(msg, v) \ 68: do { \ 69: std::ostringstream s; \ 70: s << msg; \ 71: v = s.

在MSVC 9.0下,这将失败。在g++下编译。如果我们取出宏,则编译非宏版本76-79。有什么想法吗

03: #include <iostream>
04: #include <sstream>
67: #define MAKESTRING(msg, v) \
68:        do { \
69:          std::ostringstream s; \
70:          s << msg; \ 
71:          v = s.str(); \
72:        } while(false)
73:        
74:        int main(void)
75:        { 
76:          std::ostringstream oss;
77:          std::string str;
78:          oss << "foo" << "bar";
79:          str = oss.str();
80:        
81:          MAKESTRING("foo" << "bar", str);
82:         }

testenv.cpp(71) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
testenv.cpp(71) : error C2065: 's' : undeclared identifier
testenv.cpp(71) : error C2228: left of '.str' must have class/struct/union
1>        type is ''unknown-type''
testenv.cpp(72) : error C2059: syntax error : '}'
testenv.cpp(72) : error C2143: syntax error : missing ';' before '}'
testenv.cpp(72) : error C2059: syntax error : '}'
testenv.cpp(75) : error C2143: syntax error : missing ';' before '{'
testenv.cpp(75) : error C2447: '{' : missing function header (old-style formal list?)
testenv.cpp(81) : error C2017: illegal escape sequence
testenv.cpp(126) : fatal error C1004: unexpected end-of-file found
03:#包括
04:#包括
67:#定义MAKESTRING(msg,v)\
68:do{\
69:std::ostringstream s\

70:s我会确保用于分隔宏行的反斜杠后没有任何尾随空格。由于编译器报告的行号在宏定义内,这意味着预处理器没有完成预期的操作

还可以尝试使用MSVC
/E
compile选项运行此命令,以查看预处理源代码的外观


实际上,即使在你粘贴到问题中的源代码中,第70行也有一个尾随空格。:)

很好。我想VC++在这方面更符合要求。打开view whitespace.CTRLR CTRLW非常有用。设置颜色,使其可见,但不会分散注意力。我在黑色背景上有一个深灰色。