-1

我已经声明了一个枚举如下:

enum fileType {typeA, typeB};

当我尝试将 directoryType 类型附加到字符串时,这会导致错误。我相信我需要在枚举声明中包含枚举标识符的基础类型。或者类似的东西

enum fileType : string {typeA, typeB}; 

http://msdn.microsoft.com/en-us/library/2dzy4k6e(v=vs.80).aspx中所述

但是,这不是为我编译的。声明枚举标识符的基础类型的正确语法是什么?

4

1 回答 1

1

您可能只有整数类型作为枚举的基础类型。这意味着有符号和无符号类型,例如char short intand long

枚举的名称在运行时无处可用。如果要显示它们(或附加到字符串),则必须编写特殊代码。

 enum fileType {typeA, typeB};
 const char *fileType_str[]={ "typeA","typeB"};

 fileType x = typeA;
 // display x
 std::cout << "x is " << fileType_str[x] << std::endl;

 // append x to string
 std::string y = "directoryType type to a ";
 y += fileType_str[x];
于 2012-09-23T23:40:33.470 回答