您的当前位置:首页正文

[C++]cannot jump from switch statement to this case label

来源:爱站旅游
导读[C++]cannot jump from switch statement to this case label
  switch (i) {
   case 1:
        disk d = tower1.pop();
       break;
   case 2:
        disk d = tower2.pop();
       break;
  case 3:
        disk d = tower2.pop();
       break;
   }

在SEP lab3 中,switch报错cannot jump from switch statement to this case label

查询资料有:除非使用显式的{}块,否则在一个case中声明的变量在后续的case中仍然可见,但是它们不会被初始化,因为初始化代码属于另一个case。

在这里差不多,虽然跳到了case2,但是disk d 的初始化在上面的case 已经完成了。修改方法如下,将初始化放在最上面。

       disk d;
       switch (from) {
       case 1:
            d = tower1.pop();
           break;
       case 2:
            d = tower2.pop();
           break;
      case 3:
            d = tower2.pop();
           break;

参考资料:
https://stackoverflow.com/questions/5685471/error-jump-to-case-label

因篇幅问题不能全部显示,请点此查看更多更全内容

Top