error C2361: initialization of

来自实际项目的一段代码,简化形式如下:
 switch (t)
 {
 case 0:
  int a = 0;
  break;
 default:
  break;
 }
  有什么问题吗?似乎没有。请用编译器编译一下……
  嗯?!一个错误“error C2361: initialization of 'a' is skipped by 'default' label”。这怎么可能?
  几番思琢,悟出解释:C++约定,在块语句中,对象的作用域从对象的声明语句开始直到块语句的结束,也就是说default标号后的语句是可以使用对象a的。如果程序执行时从switch处跳到default处,就会导致对象a没有被正确地初始化。确保对象的初始化可是C++的重要设计哲学,所以编译器会很严格地检查这种违例情况,像上述的示例代码中default语句后面并没有使用a,但考虑到以后代码的改动可能无意中使用,所以一样被封杀。
  明白了原因,解决起来就很容易了。只要明确地限制对象a的作用域就行了。
 switch (t)
 {
 case 0:
  { //added for fix problem
  int a = 0;
  break;
  } //added for fix problem
 default:
  break;
 }

在switch...case...结构中不能在case中定义新变量,for(int i = 0;...)
除非将定义新变量的case用块{}包住,或者选择将你的新变量在switch之前。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
You are visitor as of October 17, 1996.The Art of Assembly Language ProgrammingForward Why Would Anyone Learn This Stuff?1 What's Wrong With Assembly Language2 What's Right With Assembly Language?3 Organization of This Text and Pedagogical Concerns4 Obtaining Program Source Listings and Other Materials in This TextSection One: Machine OrganizationArt of Assembly Language: Chapter OneChapter One - Data Representation1.0 - Chapter Overview1.1 - Numbering Systems1.1.1 - A Review of the Decimal System1.1.2 - The Binary Numbering System1.1.3 - Binary Formats1.2 - Data Organization1.2.1 - Bits1.2.2 - Nibbles1.2.3 - Bytes1.2.4 - Words1.2.5 - Double Words1.3 - The Hexadecimal Numbering System1.4 - Arithmetic Operations on Binary and Hexadecimal Numbers1.5 - Logical Operations on Bits1.6 - Logical Operations on Binary Numbers and Bit Strings1.7 - Signed and Unsigned Numbers1.8 - Sign and Zero Extension1.9 - Shifts and Rotates1.10 - Bit Fields and Packed Data1.11 - The ASCII Character Set1.12 Summary1.13 Laboratory Exercises1.13.1 Installing the Software1.13.2 Data Conversion Exercises1.13.3 Logical Operations Exercises1.13.4 Sign and Zero Extension Exercises1.13.5 Packed Data Exercises1.14 Questions1.15 Programming ProjectsChapter Two - Boolean Algebra2.0 - Chapter Overview2.1 - Boolean Algebra2.2 - Boolean Functions and Truth Tables2.3 - Algebraic Manipulation of Boolean Expressions2.4 - Canonical Forms2.5 - Simplification of Boolean Functions2.6 - What Does This Have To Do With Computers, Anyway?2.6.1 - Correspondence Between Electronic Circuits and Boolean Functions2.6.2 - Combinatorial Circuits2.6.3 - Sequential and Clocked Logic2.7 - Okay, What Does It Have To Do With Programming, Then?2.8 - Generic Boolean Functions2.9 Laboratory Exercises<
回答: 引用的错误信息"Error occurred during initialization of VM java/lang/ClassNotFoundException: error in opening JAR file C:\Program Files\Java\jre1.8.0_181\lib\rt.jar"表明在初始化Java虚拟机时出现了问题,无法打开JAR文件。这可能是由于文件路径不正确或者文件不存在导致的。另外,引用的错误信息"Error occurred during initialization of VM Could not reserve enough space for object heap"是指在初始化Java虚拟机时没有足够的内存空间可供分配给对象堆。这可能是由于系统内存不足或者Java虚拟机参数配置不正确导致的。 根据你的问题"jar 包Error occurred during initialization of VM",可能是由于引用提到的JAR文件路径问题导致的错误。请检查JAR文件的路径是否正确并且文件是否存在。如果路径正确且文件存在,可以尝试重新安装Java运行时环境,并确保使用的是最新版本。如果问题仍然存在,可以尝试重新配置Java虚拟机参数,例如增加内存限制来解决引用的内存不足问题。同时,确保你的IDE(如Idea)也已正确配置和连接到Java运行时环境。 希望以上信息对解决你的问题有所帮助。如果问题仍然存在,请提供更多详细的错误信息以便更好地帮助你解决问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [java报错:Error occurred during initialization of VM](https://blog.csdn.net/zhouyingge1104/article/details/128978359)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [IDEA Error occurred during initialization of VM](https://blog.csdn.net/retire123/article/details/123767950)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值