2012-09-23 37 views
-1

的我已经宣布一个枚举如下:声明底层类型枚举标识符

enum fileType {typeA, typeB}; 

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

enum fileType : string {typeA, typeB}; 

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

描述然而,这并不编译我。声明枚举标识符的基础类型的正确语法是什么?

+6

在您的链接'这可以是任何标量类型,如int,short或long的有符号或无符号版本。 bool或char也是允许的.'你不能使用'String'或其他任何东西。 –

回答

1

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

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

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];