c ++不能出现在常量表达式中 [英] c++ cannot appear in a constant-expression|

查看:297
本文介绍了c ++不能出现在常量表达式中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:


我一直在收到一些错误,我一直在寻找一段时间,但我不知道是什么原因的错误。 (我是编程的新手。)



这里是我得到的错误:

 错误:Essais不能出现在常量表达式中(line 200)
warning:在隐式常量转换中溢出| (line 202)

我有箱子和箱子:

  char AfficherCote(int Essais)
{
char Cote;
switch(Essais)
{
(line200)case Essais< = 20:
{
(line 202)Cote ='Excellent';

return(Cote);
break;
}
case Essais< = 40:
{
Cote ='Trèsbon';
return(Cote);
break;
}
case Essais< = 60:
{
Cote ='Bon';
return(Cote);
break;
}
case Essais< = 80:
{
Cote ='Moyen';
return(Cote);
break;
}
case Essais< = 100:
{
Cote ='Muvais';
return(Cote);
break;
}
case Essais> = 100:
{
Cote ='Trèsmauvais';
return(Cote);
}
}
}


解决方案>

switch-case 只能使用常数值(*)(例如 3 'a'),而不是范围(例如< = 100 )。您还不能在 case 语句中包括变量名。正确的语法如下:

  switch(Essais)
{
case 1:
/ * ... * /
break;
case 2:
/ * ... * /
break;
默认值:
/ * ... * /
}

如果需要范围测试,请使用如果而不是 switch-case

  if(Essais <= 80)
returnCote;
else if(Essais< = 100)
returnMuvais;

另请注意,您不能使用单引号'字符串。改为使用双引号,并使用 std :: string (不

(*)



case 语句中给出的条件必须是整数类型,枚举类型或类类型的常量表达式 / em>为整数或枚举类型(详见C ++标准的§6.4.2/ 2)。


Possible Duplicate:
How do I select a range of values in a switch statement?

I've been getting some errors, and I've been searching for some time now, but I have no idea what is the cause of the errors. (I'm quite new to programming.)

Here are the errors I'm getting:

error: 'Essais' cannot appear in a constant-expression| (line 200)
warning: overflow in implicit constant conversion| (line 202)

I have case and cote:

  char AfficherCote (int Essais)
 {
 char Cote;
 switch (Essais)
  {
(line200)       case Essais<=20:
    {
(line 202)           Cote='Excellent';

        return (Cote);
        break;
    }
    case Essais<=40:
    {
        Cote='Très bon';
        return (Cote);
        break;
    }
    case Essais<=60:
    {
        Cote='Bon';
        return (Cote);
        break;
    }
    case Essais<=80:
    {
        Cote='Moyen';
        return (Cote);
        break;
    }
    case Essais<=100:
    {
        Cote='Muvais';
        return (Cote);
        break;
    }
    case Essais>=100:
    {
        Cote='Très mauvais';
        return (Cote);
    }
  }
}

解决方案

switch-case only works with constant values(*) (such as 3 or 'a'), not with ranges (such as <=100). You also must not include the variable name in the case statement. Correct syntax would be as follows:

switch (Essais)
{
case 1:
   /* ... */
  break;
case 2:
   /* ... */
   break;
default:
   /* ... */
}

If you need range tests, use if instead of switch-case:

if (Essais <= 80)
  return "Cote";
else if (Essais <= 100)
  return "Muvais";

Also note that you can't use single quotation marks ' for strings. Use double quotation marks " instead, and use variables of type std::string (not char) to store strings.


(*) To be precise, the condition given in the case statements must be a constant expression of integral type, enumeration type, or class type convertible to integer or enumeration type (see §6.4.2/2 of the C++ Standard for details).

这篇关于c ++不能出现在常量表达式中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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