为什么我会收到"错误:预期'}'"在C ++中,但不是在C? [英] Why am I getting "error: expected '}'" in C++ but not in C?
问题描述
我收到错误:预期'}'
其中,^指向当我编译下面的C ++源代码:
I'm getting "error: expected '}'"
where the '^' is pointing when I compile in the following C++ source:
typedef enum { false, true } Boolean;
^
当我编译为C源我没有得到这个错误。
I don't get this error when I compile it as C source.
什么是这个原因吗?我难倒!
What it the reason for this? I'm stumped!
推荐答案
假
和真正
是C ++的关键字,所以你不能把它们作为枚举
的标识符。
false
and true
are C++ keywords, so you can't use them as enum
identifiers.
在C他们不是关键字,让您的code将工作,但如果包括< stdbool.h>
那么它将无法编译,因为头定义假
和真正
为宏。
In C they are not keywords so your code will work, but if you include <stdbool.h>
then it will fail to compile because that header defines false
and true
as macros.
请注意,你可能不应该实现一个布尔类型自己。 C ++已经拥有了布尔
类型,如果您使用的是C99编译器,你可以包括 stdbool.h
。这会给你一个布尔
类型且具有假
和真正
值,类似于C ++。
Note that you probably shouldn't implement a boolean type yourself. C++ already has the bool
type, and if you are using a C99 compiler, you can include stdbool.h
. This will give you a bool
type that has false
and true
values, similar to C++.
这篇关于为什么我会收到&QUOT;错误:预期'}'&QUOT;在C ++中,但不是在C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!