macro "min" passed 3 arguments, but takes just 2 min(const _Tp&, const _Tp&, _Compare);

本文介绍了一种在C++项目中遇到的宏定义min冲突问题及其解决方案。该问题导致编译错误,通过在自定义头文件中取消定义min宏可以解决此问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、报错内容

In file included from /usr/include/c++/4.8.2/bits/stl_algo.h:60:0,
                 from /usr/include/c++/4.8.2/algorithm:62,
                 from ******:7:
/usr/include/c++/4.8.2/bits/algorithmfwd.h:371:41: error: macro "min" passed 3 arguments, but takes just 2
     min(const _Tp&, const _Tp&, _Compare);
                                         ^
****
/usr/include/c++/4.8.2/bits/algorithmfwd.h:367:5: error: expected unqualified-id before 鈥榗onst鈥?
     min(const _Tp&, const _Tp&);
     ^
/usr/include/c++/4.8.2/bits/algorithmfwd.h:367:5: error: expected 鈥?鈥?before 鈥榗onst鈥?
/usr/include/c++/4.8.2/bits/algorithmfwd.h:367:5: error: expected 鈥?鈥?before 鈥榗onst鈥?
/usr/include/c++/4.8.2/bits/algorithmfwd.h:367:5: error: expected 鈥?鈥?before 鈥榗onst鈥?
/usr/include/c++/4.8.2/bits/algorithmfwd.h:367:5: error: expected initializer before 鈥榗onst鈥?
In file included from /usr/include/c++/4.8.2/bits/stl_algo.h:60:0,
                 from /usr/include/c++/4.8.2/algorithm:62,
                 from ***:7:
/usr/include/c++/4.8.2/bits/algorithmfwd.h:371:5: error: template declaration of 鈥榗onst _Tp& std::min鈥?
     min(const _Tp&, const _Tp&, _Compare);
     ^
In file included from ******
/usr/include/c++/4.8.2/bits/stl_algo.h: In function 鈥榲oid std::__merge_sort_loop(_RandomAccessIterator1, _RandomAccessIterator1, _RandomAccessIterator2, _Distance)鈥?
*******:255:29: error: expected unqualified-id before 鈥?鈥?token
 #define min(a,b)            (((a) < (b)) ? (a) : (b))
                             ^
/usr/include/c++/4.8.2/bits/stl_algo.h: In function 鈥榲oid std::__merge_sort_loop(_RandomAccessIterator1, _RandomAccessIterator1, _RandomAccessIterator2, _Distance, _Compare)鈥?
*************:255:29: error: expected unqualified-id before 鈥?鈥?token
 #define min(a,b)            (((a) < (b)) ? (a) : (b))
                             ^
make: *** [***.o] Error 1
Error!

2、解决办法

这个错误与代码本身或g++版本无关,只是因为出现了宏定义冲突:

其实只需把属于自己定义的相关头文件(比如****.h)中加入(一定要加在自己的头文件引用和标准头文件的引用之间):

#include "A.h"
#undef max
#undef min

#include <algorithm>

这个问题就解决了

#define XMALLOC(type, size) mfh_malloc(type, size, NULL, __FUNCTION__, __LINE__) #define XCALLOC(type, size) mfh_calloc(type, size, NULL, __FUNCTION__, __LINE__) #define XFREE(type, ptr) mfh_free(type, ptr, __FUNCTION__, __LINE__) #define XREALLOC(type, ptr, size) mfh_realloc(type, ptr, size, NULL, NULL, __LINE__) #define XSTRDUP(type, str) mfh_strdup(type, str, NULL, __FUNCTION__, __LINE__) libedit/readline.c: In function ‘get_directory_matches’: libedit/readline.c:1553:74: error: macro "XCALLOC" passed 3 arguments, but takes just 2 1553 | matches = (char **)XCALLOC(MTYPE_TMP, match_count + 1, sizeof(char *)); | ^ In file included from /home/423251/GNOS_5_30_static/Trunk/src_2406/build/linux-arm64-charlie-sikadeer/nos/pal/linux/pal_memory.h:6, from /home/423251/GNOS_5_30_static/Trunk/src_2406/build/linux-arm64-charlie-sikadeer/nos/pal/linux/pal.h:27, from libedit/compat.h:6, from libedit/readline.c:39: /home/423251/GNOS_5_30_static/Trunk/src_2406/build/linux-arm64-charlie-sikadeer/nos/lib/memory.h:2667: note: macro "XCALLOC" defined here 2667 | #define XCALLOC(type, size) mfh_calloc(type, size, NULL, __FUNCTION__, __LINE__) | libedit/readline.c:1553:24: error: ‘XCALLOC’ undeclared (first use in this function) 1553 | matches = (char **)XCALLOC(MTYPE_TMP, match_count + 1, sizeof(char *)); | ^~~~~~~ libedit/readline.c:1553:24: note: each undeclared identifier is reported only once for each function it appears in libedit/readline.c: In function ‘filename_completion_matches’: libedit/readline.c:1828:60: error: macro "XCALLOC" passed 3 arguments, but takes just 2 1828 | matches = (char **)XCALLOC(MTYPE_TMP, 1, sizeof(char *)); | ^ In file included from /home/423251/GNOS_5_30_static/Trunk/src_2406/build/linux-arm64-charlie-sikadeer/nos/pal/linux/pal_memory.h:6, from /home/423251/GNOS_5_30_static/Trunk/src_2406/build/linux-arm64-charlie-sikadeer/nos/pal/linux/pal.h:27, from libedit/compat.h:6, from libedit/readline.c:39: /home/423251/GNOS_5_30_static/Trunk/src_2406/build/linux-arm64-charlie-sikadeer/nos/lib/memory.h:2667: note: macro "XCALLOC" defined here 2667 | #define XCALLOC(type, size) mfh_calloc(type, size, NULL, __FUNCTION__, __LINE__) | libedit/readline.c:1828:24: error: ‘XCALLOC’ undeclared (first use in this function) 1828 | matches = (char **)XCALLOC(MTYPE_TMP, 1, sizeof(char *)); | ^~~~~~~ 参数不对
最新发布
07-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值