这是一段给我错误的代码:
const char* name = pAttr->Name(); // attribute name
const char* value = pAttr->Value(); // attribute value
switch(name) // here is where error happens: must have integral or enum type
{
case 'SRAD': // distance from focal point to iso center
double D = atof(value);
break;
case 'DRAD': // distance from iso center to detector
break;
default:
break;
}
switch(name)
是发生错误的地方。它说它 必须是整数或枚举类型。那么我如何在 char*
类型上进行切换案例或等效操作?
原文由 Ono 发布,翻译遵循 CC BY-SA 4.0 许可协议
你不能在这里使用
switch
;正如错误所说,不支持const char*
。这也是一件好事,因为通过指针比较两个 C 字符串只比较 _指针_,而不是它们指向的字符串(考虑"hello" == "world"
)。即使是这样,您也试图将您的 C-string 与 multicharacter literals 进行比较,这当然不是您想要的,尤其是因为它们具有类型
int
和实现定义的值;我猜你的意思是写"SRAD"
,而不是'SRAD'
。由于您使用的是 C++,因此您应该这样做:
(I also fixed your use of
name
in the initialisation ofD
; Remy’s right — you must have meantvalue
here since"SRAD"
cannot可能被解释为double
。)