简体   繁体   English

C ++枚举[字符串]错误

[英]C++ enum [string] error

I am using an enum to declare types of variables. 我正在使用一个枚举来声明变量的类型。 However, I get an error on each string saying: Error: expression must be an integral constant expression . 但是,我在每个字符串上都收到一条错误消息: Error: expression must be an integral constant expression And if I change the "" to '' , the words which are five characters or over give the error: Error: too many characters in a character constant . 如果我改变""'' ,这是五个字符以上的话给了错误: Error: too many characters in a character constant What should I do? 我该怎么办?

typedef enum
{
    INT = "int",
    STRING = "string",
    BOOLEAN = "bool",
    CHARACTER = "char",
    DOUBLE = "double",
    FLOAT = "float",
    LONG = "long",
    SHORT = "short"
} variable_types;

I don't think enums can be mapped to characters / strings. 我认为枚举不能映射到字符/字符串。 You can use an std::map<> to map the values to string values. 您可以使用std :: map <>将值映射到字符串值。

How to easily map c++ enums to strings 如何轻松地将C ++枚举映射到字符串

The enum type wasn't made for representing strings. enum类型不是用来表示字符串的。 IF you read a bit about it, you'll notice it stores only int types inside. 如果您阅读了有关它的内容,就会发现它仅在内部存储int类型。 If you need to map values like that, enum isn't really a good choice. 如果您需要映射这样的值,那么enum并不是一个很好的选择。

If you want to keep your syntax, use the #define command: 如果要保留语法,请使用#define命令:

#define INT "int"
#define STRING "string"
...
#define SHORT "short"

You can also combine your current code with a std::map : 您还可以将当前代码与std::map

#include <map>
typedef enum
{
    INT,
    STRING,
    ...
    SHORT
} enumeratedTypes;
map<enumeratedTypes,string> variable_types;
variable_types[INT] = "int";
variable_types[STRING] = "string";
...
variable_types[SHORT] = "short";

Or just check things via an switch statement, if you need it for a function: 或者,如果需要功能,可以通过switch语句检查:

string foo(enumeratedTypes type){
   int choice = type;
   switch(choice){
   case INT:
      return "int";
   case STRING:
      return "string";
   ...
   case SHORT:
      return "short";
   default:
      return "error";
 }

Enums must be integral values. 枚举必须是整数值。 If you want, you can then use the enum values as indices into an array of strings. 如果需要,可以将枚举值用作字符串数组的索引。

Like so: 像这样:

#include <vector>
#include <string>

enum variable_types
{
    INT,
    STRING,
    BOOLEAN,
    CHARACTER,
    DOUBLE,
    FLOAT,
    LONG,
    SHORT
};

// ...

const std::vector<std::string> enumNames = { "int", "string", "bool", ... };
variable_types someEnum = ...;
auto enumName = enumNames[someEnum];

As far as I know C/CPP standard does not support string-value enumerations. 据我所知,C / CPP标准不支持字符串值枚举。 The value of the enumerations must be an integer (signed-unsigned and length depends on compiler) 枚举的值必须是整数(无符号和长度取决于编译器)

If you put your string in '' marks that means that the string shall be interpreted as a character and - of course - a character has an integer value (0-255). 如果将字符串放在''标记中,则表示该字符串应解释为字符,并且-当然-字符具有整数值(0-255)。 Since in the marks there are more than one character the compiler will complain. 由于标记中有多个字符,编译器会抱怨。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM