检查混合 C/C++ 中是否定义了 bool
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10636120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if bool is defined in mixed C/C++
提问by prelic
So I'm having issues with some code that I've inherited. This code was building fine in a C-only environment, but now I need to use C++ to call this code. The header problem.h
contains:
所以我在继承一些代码时遇到了问题。这段代码在纯 C 环境中构建得很好,但现在我需要使用 C++ 来调用这段代码。标题problem.h
包含:
#ifndef _BOOL
typedef unsigned char bool;
static const bool False = 0;
static const bool True = 1;
#endif
struct astruct
{
bool myvar;
/* and a bunch more */
}
When I compile it as C++ code, I get error C2632: 'char' followed by 'bool' is illegal
当我将它编译为 C++ 代码时,我得到 error C2632: 'char' followed by 'bool' is illegal
I get the same error if I wrap the #include "problem.h"
in extern "C" { ... }
(which I don't understand, because there should be no keyword bool
when compiling as C?)
如果我将#include "problem.h"
in包装起来,我会得到同样的错误extern "C" { ... }
(我不明白,因为bool
编译为 C 时应该没有关键字?)
I tried removing the block from #ifndef _BOOL
to #endif
, and compiling as C++, and I get errors:
我尝试从#ifndef _BOOL
to 中删除块#endif
,并编译为 C++,但出现错误:
error C2061: C requires that a struct or union has at least one member
error C2061: syntax error: identifier 'bool'
error C2061: C requires that a struct or union has at least one member
error C2061: syntax error: identifier 'bool'
I just don't understand how the C++ compiler is complaining about a redefinition of bool
, yet when I remove the redefinition and try to just use bool
to define variables, it doesn't find anything.
我只是不明白 C++ 编译器如何抱怨 的重定义bool
,但是当我删除重定义并尝试仅用于bool
定义变量时,它没有找到任何东西。
Any help is greatly appreciated.
任何帮助是极大的赞赏。
回答by Luchian Grigore
Because bool
is a basic type in C++ (but not in C), and can't be redefined.
因为bool
是 C++ 中的基本类型(但不是 C),并且不能重新定义。
You can surround your code with
你可以用你的代码包围
#ifndef __cplusplus
typedef unsigned char bool;
static const bool False = 0;
static const bool True = 1;
#endif
回答by Richard J. Ross III
You can use C99's bool
:
您可以使用 C99 的bool
:
#ifndef __cplusplus
#include <stdbool.h>
#endif
bool myBoolean; // bool is declared as either C99's _Bool, or C++'s bool data type.
Why should you use this?
为什么要使用这个?
For compatibility with other C99 code. _Bool
is commonly used in C99 Code, and is very useful. It also grants you the ability to have a boolean datatype without the need to typedef a lot of stuff, as behind the scenes, _Bool
is a datatype defined by the compiler.
与其他 C99 代码兼容。_Bool
常用在C99代码中,非常有用。它还使您能够拥有布尔数据类型,而无需 typedef 很多东西,因为在幕后,_Bool
是由编译器定义的数据类型。
回答by Component 10
You should use the __cplusplus
macro:
您应该使用__cplusplus
宏:
#ifndef __cplusplus
#ifndef _BOOL
typedef unsigned char bool;
static const bool False = 0;
static const bool True = 1;
#endif
#endif
Check out this link to the C++ FAQfor further details.
查看C++ 常见问题的链接以获取更多详细信息。
回答by Justin Hirsch
I had this "'char' followed by 'bool' is illegal" problem in VS also. The problem for me was I did not end my class declaration with a semi-colon- which I was not expecting to be the issue as this was in the header file and the problem was coming up in the cpp file! eg:
我在VS中也有这个“'char'后跟'bool'是非法的”问题。我的问题是我没有用分号结束我的类声明 - 我没想到这是问题,因为这是在头文件中,而问题出现在 cpp 文件中!例如:
class myClass
{
}; // <-- put the semi colon !!