[转载]error:jump to case label
来源:互联网 发布:黄金交易软件排名 编辑:程序博客网 时间:2024/05/01 13:57
[转载]error:jump to case label
代码:
int main()
{
int a =0;
switch(a)
{
case 0: int b = 0;break;
case 1: break;
default:break;
}
return 0;
}
编译器提示错误:
testswitch.cpp: In function ‘int main()’:
testswitch.cpp:9: error: jump to case label
testswitch.cpp:8: error: crosses initialization of ‘int b’
testswitch.cpp:10: error: jump to case label
testswitch.cpp:8: error: crosses initialization of ‘int b’
出现这样的提示,你很有可能在某个case标记中定义了局部变量,而后面还有其他的case标记或者default语句。。比如说这里的整形变量b。
看看编译器提示的信息 cross initialization of int b, 什么意思呢, 就是说跳过了变量的初始化,仔细想想,确实是这样,我们在case 0 中定义了变量b,在这个程序中,直到遇到switch的“}”右花括号,b的作用域才终结,也就是说 在case 1 和 default 分支中 变量b依然是可以访问的。考虑这样一种情况,如果switch匹配了case 1,这样case 0的代码被跳过了,那么b就没有定义,如果此时在case 1的代码中访问了b,程序会崩溃的。如果谁也不匹配,执行default也会有同样的危险。
知道了错误的原因,解决起来就很简单了
1,将case 0 标记 的代码用 {}括起来,这样b的作用域在这个花括号内。在其他的case 标记中不能访问。
2. 将 变量b放在 switch外面 定义
文章出处:http://hi.baidu.com/hins_pan/item/61ff3d8b2e072e52e63d1950
- [转载]error:jump to case label
- error: jump to case label
- error: jump to case label
- error: jump to case label
- error: jump to case label
- error:jump to case label
- 【C/C++】error: jump to case label
- symbian “error: jump to case label”
- 【C/C++】error: jump to case label
- error:jump to case label [-fpermissive]
- error: jump to case label [-fpermissive]
- error:jump to case label [-fpermissive]
- jump to case label
- error: jump to case label error: crosses initialization of xxx
- error:jump to case label error:crosses initialization of
- error:jump to case label error:crosses initialization of ' '
- error: jump to case label,crosses initialization of "xxxxxx"
- 【C++ 异常】error: jump to case label, crosses initialization
- [codeforces] 493C
- C语言学习入门 (八) 结构体和枚举
- HDU 1213 How Many Tables (并查集)
- 如何优雅地加载一个properties配置文件
- Java中二进制字节数组与十六进制互转
- [转载]error:jump to case label
- getline()函数的几点疑惑
- 计算机科学中最重要的32个算法
- Resource Type总结
- 黑马程序员——java基础之薄弱点总结(六)
- Maven实战(一)安装与配置
- 多线程程序中操作的原子性
- C# Webrowser 屏蔽弹出窗口
- 1000 A + B Problem