类似的错误比如
error: macro "min" passed 3 arguments, but takes just 2 error: expected ‘)’ before ‘const’ 解决方法
最近在Linux环境下编译一个C++项目,遇到了一个很奇怪的错误“:passed arguments, but takes just”,之前也从来没有遇到过这种error,google了一下,综合了一下原因,“宏定义冲突”,因为这个项目里面参加AWS的做法,将所有的宏函数放到了同一个头文件里面,然后不同的头文件又不断的include,于是检查各级的头文件,但是没有发现错误;看了一下stack overflow的解决办法一共就两种。
1、#undef
使用这个方法的前提是,假定确实因为宏定义冲突导致的这个错误,但是我基本将所有的相关头文件都检查了一遍,都没有发现宏被redefine。具体的办法就是在宏定义的末尾加上#undef 宏名字
2、在宏调用的时候将宏的名字用()括起来
举个例子,
(MIN) (1,2)
//1,2为参数
这两种方法使用都可以解决编译出现的错误,但是找了很久始终没有发现哪里的宏被定义冲突;最后我又重新检查了那个包含所有宏定义的头文件,发现里面的一个宏调用错误,就是在宏定义的时候是3个参数,但是调用的时候却只穿进去2个参数,导致这个错误;改正了后重新make,编译通过!
总结:在Google这个errors的时候,我尝试使用前两个办法解决问题;但是其实我并没有从根本上去解决这个问题,因为我并没有发现哪里的宏被重复定义了;很感谢mentor让我去找真正的错误出在那,而不是去为了解决问题而去解决问题,如果当时那样做了,很明显那个项目在使用的时候肯定是会出现bug的,那个时候将更难发现errors在哪。
最近在ubuntu下编译一个c++的solution,好几个project都得到如下的奇怪错误:
In file included from /usr/include/c++/4.6/bits/char_traits.h:41:0,
from /usr/include/c++/4.6/ios:41,
from /usr/include/c++/4.6/istream:40,
from /share1/linux_1579/include/ospace/std/istream:13,
from /share1/linux_1579/include/ospace/osstd/iterator.h:20,
from /share1/linux_1579/include/ospace/osstd/list.h:16,
from /share1/linux_1579/include/ospace/std/list:15,
from ../../include/LrwVUserVars.h:70,
from ../../include/LrwLogicMgrUtil.h:19,
from LrwLogicMgrUtil.cpp:6:
/usr/include/c++/4.6/bits/stl_algobase.h:233:56: error: macro "min" passed 3 arguments, but takes just 2
/usr/include/c++/4.6/bits/stl_algobase.h:254:56: error: macro "max" passed 3 arguments, but takes just 2
In file included from /usr/include/c++/4.6/bits/char_traits.h:41:0,
from /usr/include/c++/4.6/ios:41,
from /usr/include/c++/4.6/istream:40,
from /share1/linux_1579/include/ospace/std/istream:13,
from /share1/linux_1579/include/ospace/osstd/iterator.h:20,
from /share1/linux_1579/include/ospace/osstd/list.h:16,
from /share1/linux_1579/include/ospace/std/list:15,
from ../../include/LrwVUserVars.h:70,
from ../../include/LrwLogicMgrUtil.h:19,
from LrwLogicMgrUtil.cpp:6:
/usr/include/c++/4.6/bits/stl_algobase.h:187:5: error: expected unqualified-id before ‘const’
/usr/include/c++/4.6/bits/stl_algobase.h:187:5: error: expected ‘)’ before ‘const’
/usr/include/c++/4.6/bits/stl_algobase.h:187:5: error: expected initializer before ‘const’
/usr/include/c++/4.6/bits/stl_algobase.h:210:5: error: expected unqualified-id before ‘const’
/usr/include/c++/4.6/bits/stl_algobase.h:210:5: error: expected ‘)’ before ‘const’
/usr/include/c++/4.6/bits/stl_algobase.h:210:5: error: expected initializer before ‘const’
/usr/include/c++/4.6/bits/stl_algobase.h:233:5: error: ‘std::min’ declared as an ‘inline’ variable
/usr/include/c++/4.6/bits/stl_algobase.h:233:5: error: template declaration of ‘const _Tp& std::min’
/usr/include/c++/4.6/bits/stl_algobase.h:236:7: error: expected primary-expression before ‘if’
/usr/include/c++/4.6/bits/stl_algobase.h:236:7: error: expected ‘}’ before ‘if’
/usr/include/c++/4.6/bits/stl_algobase.h:238:7: error: expected unqualified-id before ‘return’
/usr/include/c++/4.6/bits/stl_algobase.h:254:5: error: ‘max’ declared as an ‘inline’ variable
/usr/include/c++/4.6/bits/stl_algobase.h:254:5: error: template declaration of ‘const _Tp& max’
/usr/include/c++/4.6/bits/stl_algobase.h:257:7: error: expected primary-expression before ‘if’
/usr/include/c++/4.6/bits/stl_algobase.h:257:7: error: expected ‘}’ before ‘if’
/usr/include/c++/4.6/bits/stl_algobase.h:259:7: error: expected unqualified-id before ‘return’
/usr/include/c++/4.6/bits/stl_algobase.h:260:5: error: expected declaration before ‘}’ token
花了很多时间都没有找到解决方法。终于在网上找到一点线索,然后一点一点试找到了一个workaround。其实这个错误与代码本身或g++版本无关,只是因为出现了宏定义冲突:
所以只需把属于自己定义的相关头文件(比如上面的LrwVUserVars.h)中加入(最好加在自己的头文件引用和标准头文件的引用之间):
#include "LrwDomBase.h"
#include "LrwPostId.h"
//added by andy
#undef max
#undef min
#include <map>
#include <queue>
则这个问题就不会再出现了。