Home  >  Article  >  Backend Development  >  C++ error: Declaration of enumerations of non-integer types is not allowed. How should I modify it?

C++ error: Declaration of enumerations of non-integer types is not allowed. How should I modify it?

王林
王林Original
2023-08-22 17:42:32893browse

C++ error: Declaration of enumerations of non-integer types is not allowed. How should I modify it?

C is a commonly used programming language that is often used in program design and development. When writing programs in C, you often encounter various compilation errors and warnings. Among them, when we use enumeration types, we sometimes encounter the error "Declaration of enumerations of non-integer types is not allowed". How to deal with this situation? This article will explain what C's enumeration types are and how to fix this error.

C enumeration type

Let’s first briefly introduce the enumeration type in C (also called enumeration variable). In C, enumeration types can define some values ​​as an enumerable list, these values ​​are called enumeration constants. An example is given below:

enum Color {RED, BLUE, GREEN};

Here we define an enumeration type Color, and define its value as RED, BLUE, and GREEN. Each enum constant here is actually an integer value. By default, the value of RED is 0, the value of BLUE is 1, and the value of GREEN is 2.

In this way, we can easily use different values ​​as enumeration constants, making the program clearer and easier to understand. For example, attributes such as day of the week or color can be defined this way.

Declaration of enumerations of non-integer types is not allowed

However, sometimes when we define an enumeration type, we will encounter an error "Declaration of enumerations of non-integer types is not allowed". For example:

enum Fruit {APPLE, BANANA, ORANGE, MANGO} f;

Here we define an enumeration type Fruit, and define its value as APPLE, BANANA, ORANGE, MANGO, etc., and also define an enumeration variable f.

However, when we compile the program, we will find a compilation error:

error: enumeration type 'Fruit' is not allowed as underlying type of enumeration

The reason for this error is that the enumeration type before the C 11 standard requires that the enumeration constant must be an integer type. In this example, although we did not explicitly assign a value to each enumeration constant (the default value is an integer type), the Fruit enumeration type we declared was a non-integer type, so the compiler reported an error.

Solution

So, how to modify this error? We can solve this problem by using C 11's new "enum class". The enumeration class is one of the new knowledge points in C 11. Compared with the previous enumeration types, there are some differences in the definition and use.

For example, we can modify the above Fruit enumeration type as follows:

enum class Fruit {APPLE, BANANA, ORANGE, MANGO};

Here we use the enum class keyword to define Fruit as an enumeration class. Unlike the previous enumeration types, the enumeration constants in the enumeration class are no longer of type int. In this way, even if each enumeration constant is not explicitly assigned a value, compilation errors will not occur.

Note that enumeration constants defined using enumeration classes need to be accessed using the scope specifier (::). For example:

enum class Fruit {APPLE, BANANA, ORANGE, MANGO};
Fruit f = Fruit::BANANA;

At this time, the value of f should be the value of the BANANA constant in the Fruit enumeration class. In this way, we avoid the error "Declaration of enumerations of non-integer types is not allowed" when defining an enumeration type.

Conclusion

In C, the enumeration type is a commonly used data type, which can easily define some values ​​as an enumerable list. However, when using enumeration types, a compilation error "Declaration of enumerations of non-integer types is not allowed" may occur. At this time, we can solve this problem by using the new enumeration class in C 11 to avoid compilation errors when defining enumeration types.

The above is the detailed content of C++ error: Declaration of enumerations of non-integer types is not allowed. How should I modify it?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn