error:crosses initialization of ...
switch(c)
{
case 0x01:
int temp = a + b;
....
break;
case 0x02:
break;
default:break;
}
此时会报如题所示错误
原因是因为C和C++中,一个变量的生命期(作用域)是这么规定的,中文还不好解释,英文原文是这样的:The scope of a variable extends from the point where it is defined to the first closing brace that matches the closest opening brace before before the variable was defined.,原文http://zhubangbing.blog.163.com/blog/static/5260927020109821055992/,上面的代码中这样写,在case 0x02中temp仍然有效,看看编译器提示的信息 cross initialization of int temp, 什么意思呢, 就是说跳过了变量的初始化,仔细想想,确实是这样,我们在case 1中定义了变量temp,在这个程序中,直到遇到switch的“}”右花括号,temp的作用域才终结,也就是说 在case 2 和 default 分支中 变量temp依然是可以访问的。考虑这样一种情况,如果switch匹配了case 2,这样case 1的代码被跳过了,那么temp就没有定义,如果此时在case 2的代码中访问了temp,程序会崩溃的。所以上面的程序应写成如下方式
switch(c)
{
case 0x01:
{
int temp = a + b;
....
}//这样的话temp的生命期到这里就结束了,在后面的case中temp就是未定义的,如果用到,编译阶段就会有提示
break;
case 0x02:
break;
default:break;
}
http://zhubangbing.blog.163.com/blog/static/526092702011931821900/
https://blog.csdn.net/zzwdkxx/article/details/27561393
error:crosses initialization of ...的更多相关文章
- error:crosses initialization of ...的解决办法
switch(c) { case 0x01: int temp = a + b; .... break; case 0x02: break; default:break; } 此时会报如题所示错误 原 ...
- [ERROR] Terminal initialization failed; falling back to unsupported java.lang.IncompatibleClassChangeError: Found class jline.Terminal, but interface was expected
1:出现此种错误应该是jar版本包冲突了,启动hive的时候,由于hive依赖hadoop,启动hive,会将hadoop的配置以及jar包等等导入到hive中,导致jar包版本冲突,下面贴一下错误, ...
- [ERROR] Terminal initialization failed; falling back to unsupported
Logging initialized using configuration in jar:file:/usr/local/hive/lib/hive-common-1.2.2.jar!/hive- ...
- goto语句引起的crosses initialization of XXX
1. 背景 goto语句虽然目前已经不提倡使用,但是用起来还是很方便,尤其是老代码中见的比较多. 在改动有goto语句的老代码时需要特别注意,是否跳过来资源的释放.有用变量的初始化等等. 很久之前写c ...
- Ribbon Workbench The plug-in execution failed because the Sandbox Client encountered an error during initialization
使用 Ribbon Workbench打开解决方案时,出现The plug-in execution failed because the Sandbox Client encountered an ...
- crosses initialization of “XXX” 的问题
在switch-case中定义了变量,用g++编译的时候报错crosses initialization of “XXX” ,在网上一查,说是gcc要求变量的定义不能位于goto之后 将变量定义放在s ...
- getting session bus failed: //bin/dbus-launch terminated abnormally with the following error: Autolaunch error: X11 initialization failed.
今天在调试dbus程序的时候,运行程序出现了getting session bus failed: //bin/dbus-launch terminated abnormally with the f ...
- Application MyTest has not been registered. This is either due to a require() error during initialization or failure to call AppRegistry.registerComponent.
运行react-native项目时报错. 说明一下:项目本来是好的,再次运行就报错了 解决解决办法倒是有,不过具体什么原因不知道.希望有知道具体原因的童鞋能够补充一下 第一种情况:真的是注册的时候写错 ...
- ERROR: unable to bind listening socket for address '127.0.0.1:9000': Address already in use (98) [30-Jan-2018 16:12:27] ERROR: FPM initialization failed解决方法
1.php启动之后发现访问nginx出现502错误,检查nginx.conf发现指定的php socket不存在 2.解决方法nginx修改陈这样,直接把绿色部门的socket写成本地地址+端口就可以 ...
随机推荐
- Python:matplotlib绘制条形图
条形图,也称柱状图,看起来像直方图,但完是两码事.条形图根据不同的x值,为每个x指定一个高度y,画一个一定宽度的条形:而直方图是对数据集进行区间划分,为每个区间画条形. 将上面的代码稍微修改一 ...
- python数据结构与算法第十七天【概率算法】
1. 古典概率 例如:麻将开始摸到的14张牌中无将的概率,两张相同的牌即为将,则有: 所有的情况:从136张牌中选出14张牌,为C136-14 无将的情况:将不同的牌分组,共有34组,依次取14张牌, ...
- MobX基础 ----- 类的静态属性和装饰器
当我们使用MobX的时候,首先要声明一个store, 用来保存状态,它的最基本的语法 如下: class Todo { @observable title = ""; @obser ...
- codeforces472C
Design Tutorial: Make It Nondeterministic CodeForces - 472C A way to make a new task is to make it n ...
- 【C/C++】动态内存分配和链表
本文对链表以及C/C++中的动态链表做详细诠释. 什么是链表? 链表是一种重要的数据结构,它最大的优点是可以进行动态的存储分配.链表有单向链表,双向链表,循环链表.对于c,这里我们只讨论单向链表. 我 ...
- Civil 3D 二次开发 创建AutoCAD对象—— 00 ——
不积跬步无以至千里,不积小流无以成江海.虽然创建一条直线.添加一个图层这样的小程序没有什么实际意义(内部命令很简单就可以完成),但对于初学二次开发的您来说,这可是一大步,这一步跨出去,您就跨进了二次开 ...
- 解决Error:com.intellij.util.indexing.StorageException
删除 C:\Users\Nihaorz\.IntelliJIdea2017.1\system\compile-server 目录下的所有内容即可
- SpringBoot部署jar与war
jar部署与启动/关闭 1.打包 clean 清理已有target目录 package 重新打包 获取打包路径,通过 scp命令发送到服务器端,scp -P ${port} ${.jar} ${use ...
- C/C++ 程序库
C/C++ 程序库 // --------------------------------------------- 来几个不常见但是很变态的库吧: bundle: 把几乎所有常见的压缩库封装成了一个 ...
- MT【273】2014新课标压轴题之$\ln2$的估计
已知函数$f(x)=e^x-e^{-x}-2x$(1)讨论$f(x)$的单调性;(2)设$g(x)=f(2x)-4bf(x),$当$x>0$时,$g(x)>0,$求$b$的最大值;(3)已 ...