error: jump to case label [-fpermissive]
来源:互联网 发布:cad网络电子版图纸 编辑:程序博客网 时间:2024/05/21 19:41
代码:
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 [-fpermissive]
- error: jump to case label [-fpermissive]
- error:jump to case label [-fpermissive]
- 编译错误 jump to case label [-fpermissive]
- error: jump to label ‘XXXX’ [-fpermissive]
- error: jump to label ‘xxxxx’ [-fpermissive]
- switch 语句:error : crosses initialization of 'xxx' and jump to case label [-fpermissive]
- qt编程中遇到的bug之error: jump to case label [-fpermissive]
- 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
- jump to case label
- 初来乍到!
- Android Api Demos登顶之路(四十八)Menu
- 黑马程序员——ios基础---Objective-C:构造方法、@property、MRC
- Linux通过chmod命令改变文件权限
- 慢慢说CSS第三天
- error: jump to case label [-fpermissive]
- Android handleMessage 的简单用法案例
- 8.25学习总结
- 几个将汉字转换成拼音的在线工具
- (一)新浪微博_OAuth2.0授权机制
- 【LeetCode-面试算法经典-Java实现】【201-Bitwise AND of Numbers Range(范围数位与结果)】
- BZOJ 2622 [2012国家集训队测试]深入虎穴 SPFA
- eclipse安装subversion(SVN)版本控制插件
- 【LeetCode-面试算法经典-Java实现】【202-Happy Number(开心数字)】