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 は、私のマルコ定義の効果をキャンセルしました。

ただし、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

以下に 3 つの解決策を示します (優先度の高い順)。

  1. #defineあなたの下にあなたを移動します#include
  2. マクロの代わりに関数/テンプレートを使用します。
  3. std::min標準<algorithm>ヘッダーにある を使用します。
于 2013-07-09T03:57:46.907 に答える