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

C/C++变量作用域详解
本文详细解析了C/C++中变量作用域的问题,特别是针对switch-case结构中局部变量的有效范围进行了深入探讨。通过一个具体例子展示了不当的变量声明可能导致的编译错误,并给出了正确的解决方案。

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.,上面的代码中这样写,在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;

}

出现错误了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
====================[ 构建 | videoProj | Debug-rk ]=============================== /usr/bin/cmake --build /home/ly/project/videoTest/cmake-build-debug-rk --target videoProj -- -j 3 Scanning dependencies of target videoProj [ 33%] Building CXX object CMakeFiles/videoProj.dir/main.cpp.o /home/ly/project/videoTest/main.cpp: In function 'int main()': /home/ly/project/videoTest/main.cpp:488:35: error: 'MPP_ENC_SET_WIDTH' was not declared in this scope; did you mean 'MPP_ENC_SET_SPLIT'? 488 | mpp_enc->control(encoder_ctx, MPP_ENC_SET_WIDTH, param); | ^~~~~~~~~~~~~~~~~ | MPP_ENC_SET_SPLIT /home/ly/project/videoTest/main.cpp:490:35: error: 'MPP_ENC_SET_HEIGHT' was not declared in this scope; did you mean 'MPP_ENC_SET_SPLIT'? 490 | mpp_enc->control(encoder_ctx, MPP_ENC_SET_HEIGHT, param); | ^~~~~~~~~~~~~~~~~~ | MPP_ENC_SET_SPLIT /home/ly/project/videoTest/main.cpp:492:35: error: 'MPP_ENC_SET_FPS' was not declared in this scope; did you mean 'MPP_ENC_SET_CFG'? 492 | mpp_enc->control(encoder_ctx, MPP_ENC_SET_FPS, param); | ^~~~~~~~~~~~~~~ | MPP_ENC_SET_CFG /home/ly/project/videoTest/main.cpp:494:35: error: 'MPP_ENC_SET_BITRATE' was not declared in this scope; did you mean 'MPP_ENC_SET_USERDATA'? 494 | mpp_enc->control(encoder_ctx, MPP_ENC_SET_BITRATE, param); | ^~~~~~~~~~~~~~~~~~~ | MPP_ENC_SET_USERDATA /home/ly/project/videoTest/main.cpp:584:65: error: 'MPP_PACKET_FLAG_INTRA' was not declared in this scope 584 | pkt->flags = (mpp_packet_get_flag(enc_packet) & MPP_PACKET_FLAG_INTRA) ? | ^~~~~~~~~~~~~~~~~~~~~ /home/ly/project/videoTest/main.cpp:604:1: error: jump to label 'CLEANUP' 604 | CLEANUP: | ^~~~~~~ /home/ly/project/videoTest/main.cpp:471:14: note: from here 471 | goto CLEANUP; | ^~~~~~~ /home/ly/project/videoTest/main.cpp:479:9: note: crosses initialization of 'int bitrate' 479 | int bitrate = 4000000; // 4Mbps | ^~~~~~~ /home/ly/project/videoTest/main.cpp:478:9: note: crosses initialization of 'int fps' 478 | int fps = 30; | ^~~ /home/ly/project/videoTest/main.cpp:477:9: note: crosses initialization of 'int height' 477 | int height = 1080; | ^~~~~~ /home/ly/project/videoTest/main.cpp:476:9: note: crosses initialization of 'int width' 476 | int width = 1920; | ^~~~~ /home/ly/project/videoTest/main.cpp:475:14: note: crosses initialization of 'void* param' 475 | MppParam param = nullptr; | ^~~~~ /home/ly/project/videoTest/main.cpp:604:1: error: jump to label 'CLEANUP' 604 | CLEANUP: | ^~~~~~~ /home/ly/project/videoTest/main.cpp:467:14: note: from here 467 | goto CLEANUP; | ^~~~~~~ /home/ly/project/videoTest/main.cpp:479:9: note: crosses initialization of 'int bitrate' 479 | int bitrate = 4000000; // 4Mbps | ^~~~~~~ /home/ly/project/videoTest/main.cpp:478:9: note: crosses initialization of 'int fps' 478 | int fps = 30; | ^~~ /home/ly/project/videoTest/main.cpp:477:9: note: crosses initialization of 'int height' 477 | int height = 1080; | ^~~~~~ /home/ly/project/videoTest/main.cpp:476:9: note: crosses initialization of 'int width' 476 | int width = 1920; | ^~~~~ /home/ly/project/videoTest/main.cpp:475:14: note: crosses initialization of 'void* param' 475 | MppParam param = nullptr; | ^~~~~ /home/ly/project/videoTest/main.cpp:604:1: error: jump to label 'CLEANUP' 604 | CLEANUP: | ^~~~~~~ /home/ly/project/videoTest/main.cpp:461:14: note: from here 461 | goto CLEANUP; | ^~~~~~~ /home/ly/project/videoTest/main.cpp:479:9: note: crosses initialization of 'int bitrate' 479 | int bitrate = 4000000; // 4Mbps | ^~~~~~~ /home/ly/project/videoTest/main.cpp:478:9: note: crosses initialization of 'int fps' 478 | int fps = 30; | ^~~ /home/ly/project/videoTest/main.cpp:477:9: note: crosses initialization of 'int height' 477 | int height = 1080; | ^~~~~~ /home/ly/project/videoTest/main.cpp:476:9: note: crosses initialization of 'int width' 476 | int width = 1920; | ^~~~~ /home/ly/project/videoTest/main.cpp:475:14: note: crosses initialization of 'void* param' 475 | MppParam param = nullptr; | ^~~~~ /home/ly/project/videoTest/main.cpp:604:1: error: jump to label 'CLEANUP' 604 | CLEANUP: | ^~~~~~~ /home/ly/project/videoTest/main.cpp:457:14: note: from here 457 | goto CLEANUP; | ^~~~~~~ /home/ly/project/videoTest/main.cpp:479:9: note: crosses initialization of 'int bitrate' 479 | int bitrate = 4000000; // 4Mbps | ^~~~~~~ /home/ly/project/videoTest/main.cpp:478:9: note: crosses initialization of 'int fps' 478 | int fps = 30; | ^~~ /home/ly/project/videoTest/main.cpp:477:9: note: crosses initialization of 'int height' 477 | int height = 1080; | ^~~~~~ /home/ly/project/videoTest/main.cpp:476:9: note: crosses initialization of 'int width' 476 | int width = 1920; | ^~~~~ /home/ly/project/videoTest/main.cpp:475:14: note: crosses initialization of 'void* param' 475 | MppParam param = nullptr; | ^~~~~ make[3]: *** [CMakeFiles/videoProj.dir/build.make:63: CMakeFiles/videoProj.dir/main.cpp.o] Error 1 make[2]: *** [CMakeFiles/Makefile2:76: CMakeFiles/videoProj.dir/all] Error 2 make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/videoProj.dir/rule] Error 2 make: *** [Makefile:118: videoProj] 上述代码编译报错,如何解决
07-26
E:\sketch_aug11a\sketch_aug11a.ino: In function 'void switch_callback(const String&)': E:\sketch_aug11a\sketch_aug11a.ino:428:3: error: 'BAFA_LOG' was not declared in this scope 428 | BAFA_LOG("get switch state: ", state); // 打印接收到的状态 | ^~~~~~~~ E:\sketch_aug11a\sketch_aug11a.ino: In function 'void duerQuery(int32_t)': E:\sketch_aug11a\sketch_aug11a.ino:525:5: error: jump to case label 525 | default : // 默认情况返回功率状态 | ^~~~~~~ E:\sketch_aug11a\sketch_aug11a.ino:522:14: note: crosses initialization of 'String duerTopic' 522 | String duerTopic = "dueros/" + String(bafa_id); | ^~~~~~~~~ E:\sketch_aug11a\sketch_aug11a.ino:520:14: note: crosses initialization of 'String state' 520 | String state = (oDuerState == YYXBC_HIGH) ? "on" : "off"; | ^~~~~ E:\sketch_aug11a\sketch_aug11a.ino: In function 'void miotQuery(int32_t)': E:\sketch_aug11a\sketch_aug11a.ino:556:5: error: jump to case label 556 | default : // 默认情况返回功率状态 | ^~~~~~~ E:\sketch_aug11a\sketch_aug11a.ino:553:14: note: crosses initialization of 'String miotTopic' 553 | String miotTopic = "miot/" + String(bafa_id); | ^~~~~~~~~ E:\sketch_aug11a\sketch_aug11a.ino:551:14: note: crosses initialization of 'String state' 551 | String state = (oMioState == YYXBC_HIGH) ? "on" : "off"; | ^~~~~ E:\sketch_aug11a\sketch_aug11a.ino: In function 'void aligenieQuery(int32_t)': E:\sketch_aug11a\sketch_aug11a.ino:587:5: error: jump to case label 587 | default : // 默认情况返回功率状态 | ^~~~~~~ E:\sketch_aug11a\sketch_aug11a.ino:584:14: note: crosses initialization of 'String aligenieTopic' 584 | String aligenieTopic = "aligenie/" + String(bafa_id); | ^~~~~~~~~~~~~ E:\sketch_aug11a\sketch_aug11a.ino:582:14: note: crosses initialization of 'String state' 582 | String state = (oAligenieState == YYXBC_HIGH) ? "on" : "off"; | ^~~~~ Multiple libraries were found for "WiFiUdp.h" Used: C:\Users\Administrator\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\libraries\ESP8266WiFi Not used: C:\Users\Administrator\Documents\Arduino\libraries\WiFiNINA Not used: C:\Users\Administrator\Documents\Arduino\libraries\WiFi101_Generic Not used: C:\Users\Administrator\Documents\Arduino\libraries\WiFiEspAT Multiple libraries were found for "EEPROM.h" Used: C:\Users\Administrator\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\libraries\EEPROM Not used: C:\Users\Administrator\Documents\Arduino\libraries\ATMAC_EEPROM Multiple libraries were found for "PubSubClient.h" Used: C:\Users\Administrator\Documents\Arduino\libraries\PubSubClient Not used: C:\Users\Administrator\Documents\Arduino\libraries\TBPubSubClient Not used: C:\Users\Administrator\Documents\Arduino\libraries\PubSubClient3 exit status 1 Compilation error: 'BAFA_LOG' was not declared in this scope
最新发布
09-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值