jump to case label
来源:互联网 发布:齐鲁软件大赛 编辑:程序博客网 时间:2024/05/19 07:10
代码:
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外面 定义。
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外面 定义。
- 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
- 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 case label error: crosses initialization of xxx
- error:jump to case label error:crosses initialization of
- error: jump to case label,crosses initialization of "xxxxxx"
- error:jump to case label error:crosses initialization of ' '
- bundle 使用方法
- 网站链接点击如何分析
- 小公司技术管理者的点滴--盲目的自信和一系列微小措施
- PerformanceCounter获取的CPU使用率但总是0
- EXTJS初级UI设计进阶阶段
- jump to case label
- 【Cantor 的数表 5.4.1】
- Android之开发常用颜色
- JTester配置
- java集合(记录)
- return false 正确运用
- Android
- 《C++必知必会》读书笔记(上)
- 进程通信