[C/C++] error: jump to case label, error: crosses initialization of ‘int a’

本文探讨了C++中switch-case语句的正确使用方法,特别关注变量声明及初始化的问题。解释了为什么在case分支中直接初始化变量会导致编译错误,并提供了安全的编码实践建议。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

	switch (t)
	{
		case 0:
			int a = 0;
			break; // NOT OK
		case 1:
			int b;
			b = 0;
			break; // OK
		default:
			break;
	}

C++ case语句当中不能出现带初始化的变量声明,只能出现POD类型,同时不带初始化的变量声明。

C++ Standard当中已经说明这一点。

It is possible to transfer into a block, but not in a way that bypasses declarations with initialization. A program that jumps(77) from a point where a local variable with automatic storage duration is not in scope to a point where it is in scope is ill-formed unless the variable has POD type (3.9) and is declared without an initializer (8.5).
(*) The transfer from the condition of a switch statement to a case label is considered a jump in this respect.

比较安全的情况,一般是用花括号限制变量的scope。

针对上面代码片段case 0和case 1的个人看法:

对于int a = 0;的形式,其scope在case 0和case 1都有作用,但是在declare的时候给了一个初值,所以在case 0和case 1的使用方式就不同了。C++当中的case分支可以看成是labels,case 0和case 1是等同的并列关系,但是现在case 0有初始化,case 1没有初始化,使用方式不同了,所以编译器会提示错误。

对于int b; b = 0;的形式,其scope在case 1和default都有作用,declare的时候没有给初始,是一个不确定的值。case 1当中后面b = 0;可以看成是对b变量的使用(赋值),在default分支也可以对b进行赋值等操作,在case 1和default对b的使用方式是相同的,所以没有提示错误。

参考:

http://stackoverflow.com/questions/3757445/switch-case-declaration-with-initialization-declaration-and-then-assignment

http://stackoverflow.com/questions/92396/why-cant-variables-be-declared-in-a-switch-statement

出现错误了37266687.cpp: In function ‘int main()’: 37266687.cpp:31:22: error: jump to case label 31 | case '-': | ^~~ 37266687.cpp:28:25: note: crosses initialization ofint c’ 28 | int c = n.top(); n.pop(); | ^ 37266687.cpp:27:25: note: crosses initialization ofint b’ 27 | int b = n.top(); n.pop(); | ^ 37266687.cpp:37:22: error: jump to case label 37 | case '*': | ^~~ 37266687.cpp:28:25: note: crosses initialization ofint c’ 28 | int c = n.top(); n.pop(); | ^ 37266687.cpp:27:25: note: crosses initialization ofint b’ 27 | int b = n.top(); n.pop(); | ^ 37266687.cpp:43:22: error: jump to case label 43 | case '/': | ^~~ 37266687.cpp:28:25: note: crosses initialization ofint c’ 28 | int c = n.top(); n.pop(); | ^ 37266687.cpp:27:25: note: crosses initialization ofint b’ 27 | int b = n.top(); n.pop(); | ^ 37266687.cpp:49:22: error: jump to case label 49 | case ' ': | ^~~ 37266687.cpp:28:25: note: crosses initialization ofint c’ 28 | int c = n.top(); n.pop(); | ^ 37266687.cpp:27:25: note: crosses initialization ofint b’ 27 | int b = n.top(); n.pop(); |请修改#include <iostream> #include <stack> #include <cctype> using namespace std; int main() { stack<int> n; char s; int a = 0; // 当前操作数 bool inNum = false; // 标记是否在读取数字 while ((s = getchar()) != '@') { if (isdigit(s)) { a = a * 10 + (s - '0'); inNum = true; } else { // 遇到非数字字符时,将当前操作数入栈(如果有) if (inNum) { n.push(a); a = 0; inNum = false; } switch (s) { case '+': if (n.size() < 2) break; int b = n.top(); n.pop(); int c = n.top(); n.pop(); n.push(c + b); break; case '-': if (n.size() < 2) break; b = n.top(); n.pop(); c = n.top(); n.pop(); n.push(c - b); // 注意顺序:c - b break; case '*': if (n.size() < 2) break; b = n.top(); n.pop(); c = n.top(); n.pop(); n.push(c * b); break; case '/': if (n.size() < 2) break; b = n.top(); n.pop(); c = n.top(); n.pop(); if (b != 0) n.push(c / b); // 避免除零错误 break; case ' ': // 空格不处理,已在前面的if(inNum)中处理 break; } } } // 处理最后一个操作数(如果有) if (inNum) n.push(a); if (!n.empty()) cout << n.top(); return 0; }
最新发布
07-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值