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. 如何在MAC上运行exe程序

    1. 首先下载并运行CrossOver 运行CrossOver需要收费,试用期为14天,运行CrossOver 2. 选择exe应用程序,新建容器,安装exe程序 3.安装成功后,运行exe应用程序启 ...

  2. 微信小程序支付功能

    API:wx.requestPayment() { } https://blog.csdn.net/qishubiao/article/details/80804052

  3. faster rcnn训练自己的数据集

    采用Pascal VOC数据集的组织结构,来构建自己的数据集,这种方法是faster rcnn最便捷的训练方式

  4. Real-time chart using ASP.NET Core and WebSocket

    Solution in glance The following diagram illustrates our solution where IoT device reports readings ...

  5. iOS的非常全的三方库,插件,大牛博客

    转自: http://www.cnblogs.com/zyjzyj/p/6015625.html github排名:https://github.com/trending, github搜索:http ...

  6. if 结构语句

    if 条件: print()#不只是能输入print

  7. Nginx 优化缓冲区与传输效率

    L:126 这里简单的做个计算 比如 我的服务器带宽是 5M=41943040字节 如果按照公网用PIND的得到延迟结果 icmp_seq=3 ttl=49 time=35.612 ms BDP =  ...

  8. Android与H5交互 原理与对比

    原文:  https://www.jianshu.com/p/345f4d8a5cfa 1.Android调用JS的方法有2种: (1)通过WebView的loadUrl() // 调用js中的函数: ...

  9. qt 在窗口上画框

    在窗口w上面画个黄色的框:在窗口上添加一个label,然后在label上画框 QLabel label(&w); label.setScaledContents(true); QPixmap ...

  10. #SQL1242错误

    --------------------- 子查询更新数据时遇到多条数据时,可以使用SUM&MIN等函数解决:如下: //正确的方法一对多 UPDATE `yd_draw_prize_orde ...