宏的实际参数太多? [英] Too many actual parameters for macro?
问题描述
代码:
#include <iostream>
using namespace std;
#define ADD(x,y) ((x)+(y))
int main( int argc, char** argv )
{
cout << ADD(1,2,) << endl;
return 0;
}
编译器输出:
1>正在编译...
1> main.cpp
1> c:\warn_test\main.cpp(9):警告C4002:宏'ADD'的实际参数过多
1>Compiling...
1>main.cpp
1>c:\warn_test\main.cpp(9) : warning C4002: too many actual parameters for macro 'ADD'
为什么这不是错误?
g ++(GCC)4.2.1 20070719 [FreeBSD]
给出了更合理的输出(在我看来):
g++ (GCC) 4.2.1 20070719 [FreeBSD]
gives more reasonable (in my mind) output:
main.cpp:9:18:错误:宏 ADD传递了3个参数,但仅接受了2个
main.cpp:在函数 int main(int,char **)':
main.cpp:9:错误:在此范围内未声明'ADD'
main.cpp:9:18: error: macro "ADD" passed 3 arguments, but takes just 2
main.cpp: In function 'int main(int, char**)':
main.cpp:9: error: 'ADD' was not declared in this scope
尽管我不完全确定编译器认为第三个参数是什么。
Though I'm not entirely sure what either compiler thinks the third argument is.
编辑:添加了完整的 gcc
输出和版本信息。
Added complete gcc
output and version info.
推荐答案
您使用 ADD(1,2 ,)
,请注意第二个 。删除它,它将可以正常编译!
You use ADD(1,2,)
, note the second ,
. Remove that and it will compile just fine!
@schnaader:您说得对,我读得太快了。抱歉。
@schnaader: You are right, I read too fast. Sorry.
请提供有关所涉及编译器的更多详细信息。我使用:g ++(Ubuntu / Linaro 4.4.4-14ubuntu5)4.4.5,这是我得到的结果:
[edit] Please provide more details about the compiler in question. I use: g++ (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5, and this is the result I get:
test.cpp:9: error: macro "ADD" passed 3 arguments, but takes just 2
test.cpp: In function ‘int main(int, char**)’:
test.cpp:9: error: ‘ADD’ was not declared in this scope
[edit2]
对不起,再次有点太快了:-)。我看到您使用Visual Studio对其进行了标记。 VS比g ++更能容忍。我想是因为它在这种情况下很容易解决,所以会自动更正。
[edit2] Sorry, again a bit too fast :-). I see you tagged it with visual studio. VS is more tolerant than g++. I suppose that -- because it is easy to resolve in this case -- it automatically corrects it.
这篇关于宏的实际参数太多?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!