error: jump to case label 或者 error: crosses initialization of 编译错误

本文探讨了C++中switch语句的一个常见陷阱——在不同的case标签间定义局部变量所导致的问题,并给出了两种解决方案。

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

代码:
int main()
{
    int a =0;
    switch(a)
    {
        case 0: int b = 0;break;
        case 1: break;
        default:break;
    }
    return 0;
}

编译器提示错误:
testswitch.cpp: In function ‘int main()’:
testswitch.cpp:9: error: jump to case label
testswitch.cpp:8: H‘int b’
testswitch.cpp:10: error: jump to case label
testswitch.cpp:8: error:   crosses initialization of ‘int b’

出现这样的提示,你很有可能在某个case标记中定义了局部变量,而后面还有其他的case标记或者default语句。。比如说这里的整形变量b。

看看编译器提示的信息 cross initialization of int b, 什么意思呢, 就是说跳过了变量的初始化,仔细想想,确实是这样,我们在case 0 中定义了变量b,在这个程序中,直到遇到switch的“}”右花括号,b的作用域才终结,也就是说 在case 1 和 default 分支中 变量b依然是可以访问的。考虑这样一种情况,如果switch匹配了case 1,这样case 0的代码被跳过了,那么b就没有定义,如果此时在case 1的代码中访问了b,程序会崩溃的。如果谁也不匹配,执行default也会有同样的危险。

知道了错误的原因,解决起来就很简单了

1,将case 0 标记 的代码用 {}括起来,这样b的作用域在这个花括号内。在其他的case 标记中不能访问。

2. 将 变量b放在 switch外面 定义。
出现错误了37266687.cpp: In function ‘int main()’: 37266687.cpp:31:22: error: jump to case label 31 | case '-': | ^~~ 37266687.cpp:28:25: note: crosses initialization of ‘int c’ 28 | int c = n.top(); n.pop(); | ^ 37266687.cpp:27:25: note: crosses initialization of ‘int 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 of ‘int c’ 28 | int c = n.top(); n.pop(); | ^ 37266687.cpp:27:25: note: crosses initialization of ‘int 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 of ‘int c’ 28 | int c = n.top(); n.pop(); | ^ 37266687.cpp:27:25: note: crosses initialization of ‘int 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 of ‘int c’ 28 | int c = n.top(); n.pop(); | ^ 37266687.cpp:27:25: note: crosses initialization of ‘int 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、付费专栏及课程。

余额充值