为什么我在c++中得到" error: expected '} ",而在C中没有?

时间:2022-06-10 22:49:35

I'm getting "error: expected '}'" where the '^' is pointing when I compile in the following C++ source:

我得到“错误:预期‘}’”,“^”是指当我编译c++源代码如下:

typedef enum { false, true } Boolean;
               ^

I don't get this error when I compile it as C source.

当我编译为C源代码时,我不会得到这个错误。

What it the reason for this? I'm stumped!

原因是什么?我难住了!

3 个解决方案

#1


16  

false and true are C++ keywords, so you can't use them as enum identifiers.

false和true是c++关键字,因此不能将它们用作enum标识符。

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中,它们不是关键字,所以你的代码可以工作,但是如果你包括 。然后,它将无法编译,因为该头定义了false和true作为宏。

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++.

注意,您可能不应该自己实现布尔类型。c++已经有bool类型,如果使用C99编译器,可以包括stdbool.h。这将给您一个bool类型,它具有错误和真实的值,类似于c++。

#2


6  

To solve this you need to do:

要解决这个问题,你需要:

#ifdef __cplusplus
  typedef bool Boolean;
#else
  typedef enum { false, true } Boolean;
#endif

That way, you are not trying to use C++ keywords (true and false) in an enum.

这样,您就不会尝试在enum中使用c++关键字(true和false)。

#3


2  

The true and false are keywords in C++. You cannot use them in enum identifiers.

正误是c++中的关键字。不能在enum标识符中使用它们。

As it is said in the standard :

正如《标准》中所说:

2.12 Keywords [lex.key]

2.12关键字(lex.key)

The identifiers shown in Table 4 are reserved for use as keywords (that is, they are unconditionally treated as keywords in phase 7) except in an attribute-token.

表4中所示的标识符被保留为作为关键字使用(即在第7阶段将它们无条件地视为关键字),但属性令牌除外。

In table 4:

表4:

 false
 ...
 true

In C, they are not keywords, your code should work but the best should be to include <stdbool.h> who already defines true and false then you don't need to define them by yourself.

在C中,它们不是关键字,您的代码应该工作,但是最好应该包括 已经定义了真和假,你不需要自己定义它们。 。h>

#1


16  

false and true are C++ keywords, so you can't use them as enum identifiers.

false和true是c++关键字,因此不能将它们用作enum标识符。

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中,它们不是关键字,所以你的代码可以工作,但是如果你包括 。然后,它将无法编译,因为该头定义了false和true作为宏。

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++.

注意,您可能不应该自己实现布尔类型。c++已经有bool类型,如果使用C99编译器,可以包括stdbool.h。这将给您一个bool类型,它具有错误和真实的值,类似于c++。

#2


6  

To solve this you need to do:

要解决这个问题,你需要:

#ifdef __cplusplus
  typedef bool Boolean;
#else
  typedef enum { false, true } Boolean;
#endif

That way, you are not trying to use C++ keywords (true and false) in an enum.

这样,您就不会尝试在enum中使用c++关键字(true和false)。

#3


2  

The true and false are keywords in C++. You cannot use them in enum identifiers.

正误是c++中的关键字。不能在enum标识符中使用它们。

As it is said in the standard :

正如《标准》中所说:

2.12 Keywords [lex.key]

2.12关键字(lex.key)

The identifiers shown in Table 4 are reserved for use as keywords (that is, they are unconditionally treated as keywords in phase 7) except in an attribute-token.

表4中所示的标识符被保留为作为关键字使用(即在第7阶段将它们无条件地视为关键字),但属性令牌除外。

In table 4:

表4:

 false
 ...
 true

In C, they are not keywords, your code should work but the best should be to include <stdbool.h> who already defines true and false then you don't need to define them by yourself.

在C中,它们不是关键字,您的代码应该工作,但是最好应该包括 已经定义了真和假,你不需要自己定义它们。 。h>