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 ...的更多相关文章

  1. error:crosses initialization of ...的解决办法

    switch(c) { case 0x01: int temp = a + b; .... break; case 0x02: break; default:break; } 此时会报如题所示错误 原 ...

  2. [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包版本冲突,下面贴一下错误, ...

  3. [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- ...

  4. goto语句引起的crosses initialization of XXX

    1. 背景 goto语句虽然目前已经不提倡使用,但是用起来还是很方便,尤其是老代码中见的比较多. 在改动有goto语句的老代码时需要特别注意,是否跳过来资源的释放.有用变量的初始化等等. 很久之前写c ...

  5. 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 ...

  6. crosses initialization of “XXX” 的问题

    在switch-case中定义了变量,用g++编译的时候报错crosses initialization of “XXX” ,在网上一查,说是gcc要求变量的定义不能位于goto之后 将变量定义放在s ...

  7. 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 ...

  8. Application MyTest has not been registered. This is either due to a require() error during initialization or failure to call AppRegistry.registerComponent.

    运行react-native项目时报错. 说明一下:项目本来是好的,再次运行就报错了 解决解决办法倒是有,不过具体什么原因不知道.希望有知道具体原因的童鞋能够补充一下 第一种情况:真的是注册的时候写错 ...

  9. 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写成本地地址+端口就可以 ...

随机推荐

  1. 重写TreeView模板来实现数据分层展示(二)

    前面一片文章实现TreeView的基本的模板重写,那么照着这个思路,我们再来写一个稍稍复杂的TreeView ,其它的内容都和前面系列内容相似,还是和之前文章介绍的一样,首先看看做出的DEMO的最终样 ...

  2. Java第一次实训

    package com.wsy.work; public class JudgeNumber { public static void main(String[] args) { int a = 5; ...

  3. @Html自定义属性

    @Html.DropDownList("CardCode", (SelectList)ViewData["cardcodeselectlist"], " ...

  4. 学习 Spring (十六) AOP API

    Spring入门篇 学习笔记 Spring AOP API 是 Spring 1.2 历史用法,现在仍然支持 这是 Spring AOP 基础,现在的用法也是基于历史的,只是更简便了 Pointcut ...

  5. Nginx 减少关闭连接的time_wait端口数量

    L:129

  6. 添加一个Android框架层的系统服务与实现服务的回调

    2017-10-09 概述 所谓Android系统服务其本质就是一个通过AIDL跨进程通信的小Demo的延伸而已.按照 AIDL 跨进程通信的标准创建一套程序,将服务端通过系统进程来运行实现永驻内存, ...

  7. BZOJ2229[Zjoi2011]最小割——最小割树

    题目描述 小白在图论课上学到了一个新的概念——最小割,下课后小白在笔记本上写下了如下这段话: “对于一个图,某个对图中结点的划分将图中所有结点分成两个部分,如果结点s,t不在同一个部分中,则称这个划分 ...

  8. 微信小程序——常用快捷键【四】

    格式调整 ctrl+[, ctrl+]:代码行缩进(向前|向后) ctrl+shift+[, ctrl+shift+] :折叠打开代码块 ctrl+C, ctrl+V:复制粘贴,如果没有选中任何文件则 ...

  9. Lambda 动态表达式(排序)

    网上看到的: class Program { static List<User> list = new List<User>() { new User(){ID=1,Name= ...

  10. Android 取消标题栏

    有很多种方法. 但一般多个页面的话,大多会在AndroidManifest.xml文件中设置 那么这里你需要注意了. 第一种: 如果style.xml 的 parent   是 <style n ...