错误:case标签不会减少为整数常量 [英] error: case label does not reduce to an integer constant

查看:191
本文介绍了错误:case标签不会减少为整数常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  void  flight_operator :: get_define_flight_num( char  namecode){
cout<< 输入航班代码:;
cin>> namecode;
switch (namecode){
case MH case mh
cout<< Make Airlines \\\
<< endl;
break ;
case FY case fy
cout<< Fire Airlines \ n<< endl;
默认
cout<< 无效的代码!<< endl;
break ;
}

}



 

解决方案

有什么不清楚的?案例标签应为整数常量。它可以是整数类型之一,枚举类型,类似这样,但不是字符串,不是数组,或任何其他更复杂的对象或指向对象的指针。你不能使用你的MH,FY等。



你可以在成千上万的地方找到一些解释:http://bit.ly/1lLKRN4 [ ^ ]。



使用立即常量的整个想法,特别是字符串,非常糟糕。这种代码是不可支持的。这实际上是一个很大的编程反模式

http://en.wikipedia.org/wiki/Magic_string_(programming)#Magic_strings_in_code [ ^ ],

http ://en.wikipedia.org/wiki/Magic_number_(编程)#Unnamed_numerical_constants [ ^ ](是的,魔术数字也是如此);

参见:http://en.wikipedia.org/wiki/Anti-pattern#Programming [ ^ ]。



-SA

void flight_operator::get_define_flight_num(char namecode){
    cout<<"Enter flight code:  ";
    cin>>namecode;
    switch(namecode){
        case "MH": case "mh":
            cout<<"Make Airlines\n"<<endl;
            break;
        case "FY": case "fy":
            cout<<"Fire Airlines\n"<<endl;
        default:
            cout<<"Invalid code!"<<endl;
            break;
    }

}


解决方案

What is not clear? A case label should be an integer constant. It could be one of integer types, enumeration type, something like this, but not a string, not an array, or any other more complex object or a pointer to object. You cannot use your "MH", "FY" and the like.

You could find some explanation in thousands of places: http://bit.ly/1lLKRN4[^].

The whole idea of using immediate constants, especially string, it very bad. This kind of code is not supportable. This is actually a big programming anti-pattern:
http://en.wikipedia.org/wiki/Magic_string_(programming)#Magic_strings_in_code[^],
http://en.wikipedia.org/wiki/Magic_number_(programming)#Unnamed_numerical_constants[^] (yes, magic numbers, too);
see also: http://en.wikipedia.org/wiki/Anti-pattern#Programming[^].

—SA


这篇关于错误:case标签不会减少为整数常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆