Dan*_*ens 0 c++ arguments c-preprocessor
我不明白为什么下面的代码不会编译:
#include <iostream>
#define SHORT_NAME 4;
int func(int arg)
{
return arg;
}
int main()
{
return func(SHORT_NAME); // Error: expected a ')'
}
Run Code Online (Sandbox Code Playgroud)
我应该const int SHORT_NAME = 4在第2行使用吗?
从宏中删除分号,SHORT_NAME因为预处理后它会扩展为:
return func(4;);
Run Code Online (Sandbox Code Playgroud)
或者const int按照您在问题中的建议使用.有关宏与vs的讨论,请参阅"static const"vs"#define"vs"enum"const.