gpt4 book ai didi

c++ - switch 语句多字符常量

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:28:50 26 4
gpt4 key购买 nike

我正在尝试将其转换为 switch 语句

if (codeSection == 281)
cout << "bigamy";
else if (codeSection == 321 || codeSection == 322)
cout << "selling illegal lottery tickets";
else if (codeSection == 383)
cout << "selling rancid butter";
else if (codeSection == 598)
cout << "wounding a bird in a public cemetery";
else
cout << "some other crime";

// Actual switch statement
switch (codeSection)
{
case '281':
cout << "bigamy" << endl;
break;

case '321':
case '322':
cout << "selling illegal lottery tickets" << endl;
break;

case '383':
cout << "selling rancid butter" << endl;
break;

case '598':
cout << "wounding a bird in a public cemetery";
break;

default:
cout << "some other crime"<< endl;

}

编译器说 switch 语句多字符常量,并给我一个黄色警告但仍然可以编译。我的问题是这种情况应该只采用 char 形式吗?像案例'2'

最佳答案

case '281':

应该是

case 281:

其余部分也类似,否则编译器“认为”您尝试使用多字符常量,这可能不是您想要的。

case 不必是 char。事实上,它必须是与转换和整数提升后的条件类型相同类型的常量表达式,参见例如http://en.cppreference.com/w/cpp/language/switch .

关于c++ - switch 语句多字符常量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33160033/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com