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

it2023-04-17  99

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

最新回复(0)