1

我有以下代码:

#ifndef min
#define min(a,b) (((a)< (b)) ? (a) : (b))
#endif

int test(){
    return min(0,1);
}

哪个工作正常。但是,如果我包含一些头文件(来自图形数据库,可以在此处找到该头文件的内容:http ://www.sparsity-technologies.com/dex ),编译器会抱怨 min 未定义,例如Dex.h 只是取消了我的 marco 定义的效果。

但是,Dex.h 不包含任何未定义的语句。我无法移动宏定义,因为它实际上包含在另一个头文件中。

出了什么问题,我该怎么办?

#ifndef min
#define min(a,b) (((a)< (b)) ? (a) : (b))
#endif

#include "gdb/Dex.h"

int test(){
    return min(0,1);
}

我得到的编译器错误是:

 test.c:9:16: error: 'min' was not declared in this scope
4

2 回答 2

3

看起来你包括c++config.h,它说:

00307 // This marks string literals in header files to be extracted for eventual
00308 // translation.  It is primarily used for messages in thrown exceptions; see
00309 // src/functexcept.cc.  We use __N because the more traditional _N is used
00310 // for something else under certain OSes (see BADNAMES).
00311 #define __N(msgid)     (msgid)
00312 
00313 // For example, <windows.h> is known to #define min and max as macros...
00314 #undef min
00315 #undef max

进一步看,它似乎包含在包含的字符串中

# 39 "dex/includes/dex/gdb/common.h" 2
于 2013-07-09T04:26:59.867 回答
2

据推测,该头文件-#undef馈送min(直接或通过它包含的另一个头文件)。

以下是三种解决方案(按优先顺序递增):

  1. 将您移到您的#define下方#include
  2. 使用函数/模板而不是宏。
  3. 使用std::min,可以在标准<algorithm>标题中找到。
于 2013-07-09T03:57:46.907 回答