1

I have a double type bool so have added to a header:

typedef double bool;
extern bool true;
extern bool false;

with:

bool true = 1.0;
bool false = 0.0;

in the corresponding C file.

However I now have the errors multiple definition of true, and the same for false, pointing to the first line of the first function in the C file. the error that says 'previous declaration was here' points to the same line... it doesnt make any difference which function is placed first in the file it always points to it.

My header files, though included via a common header file, do have include guards so I hopefully shouldn't have multiple declaration of true and false there.

I have changed the typedef to tBool with vars tTrue and tFalse, which solves the problem, but I don't get why it occurred in the first place? As there are still some bool types using true and false in the code it seems like the compiler may have a definition for true and false as ints already... though I didn't think C did this

Im using dev-c++ 4.9.9.2 IDE that uses mingw, though Im not sure which version mingw.

Anyone know why this happened?

4

1 に答える 1

3

あなたのパラメータは実際にはブール値ではないように思えます。離散数 0.0 と 1.0 の特殊なケースを持つ浮動小数点パラメーターがあります。double型の代わりに 2 つの定数を作成します。

C99 では、タイプ_Boolマクロ boolマクロ true、およびマクロ falseの定義が追加されました。ヘッダーに次を挿入してみてください。

#if __bool_true_false_are_defined
# error "stdbool.h has been included"
#endif
#ifdef bool
# error "bool is already DEFINED"
#endif
#ifdef true
# error "true is already DEFINED"
#endif
#ifdef false
# error "false is already DEFINED"
#endif

これらのいずれかが発火した場合は、stdbool.hをどこかに含めています。#undef3 つのマクロを使用して、タイプを安全にセットアップできるはずです。もちろん、他の誰かがbool小さな整数値であると予想し、あなたがスティックを振ることができるより多くのスタイルの問題を抱えている場合、これはおそらく壊れます.

ISO/IEC 9899:1999 は、標準に追加される前に、多くのグループが独自のブール型を既に定義しているという事実に譲歩しています。boolこれが、 、true、およびfalseを新しいキーワードではなくマクロとして定義する理由です。ただし、次の警告が明示的に含まれています。

7.26.7 ブール型と値<stdbool.h>

boolマクロ、true、およびを未定義にしてからおそらく再定義するfalse機能は廃止された機能です。

于 2012-07-23T12:18:43.190 に答える