c - 为什么对 switch case 值进行“或”运算时会出现错误?

标签 c visual-studio visual-studio-2019

 switch (ch)
    {
    case '~' || 177: 
        tile->tileType = TILE_NONE;
        return true;
    case '@' || 219: //error here: '@'
        tile->tileType = TILE_WALL;
        return true; 
    }

错误如下: “案例值‘1’已被使用”

......到底是什么??

我尝试清理解决方案,重新启动,使用另一台电脑。我“已经很接近”重新安装 VS 了。知道发生了什么吗?

最佳答案

不能像 if 语句那样将多个 case 除以 ||。你有这样的情况

'~' || 177:

这是一个计算结果为 true 的 bool 表达式,which gets evaluated to 1因为 case 只能是整型(intcharenum),所以必须转换为 int.所以你说的是真的

case 1:

同样的事情发生在你的第二个 case 语句中,它被评估为第二个 case 1:,因此出现错误

而是这样做:

case '~':
case 117:
   //code

关于c - 为什么对 switch case 值进行“或”运算时会出现错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59432466/

相关文章:

C 术语 : header vs. 签名

visual-studio - 我想在 4 个工作站上使用 VS 2010 Pro,哪种 MSDN 订阅或许可模式最好?

xaml - MSBuild :UpdateDesignTimeXaml do? 是什么

c++ - 声明使用 C 代码的 c++ 类的多个实例

c - UNIX C 解析文件

当用户输入无效字符时,C 程序不会结束

visual-studio - 更改 Visual Studio 2015 键绑定(bind)

c++ - 链接到MS Visual C上的protobuf 3时出错

visual-studio-2019 - Visual Studio Professional 2019 版本 16.10.0 无法在 SSDT 项目中打开 .sql 文件

debugging - Visual Studio 2019 具有多条调试黄线