在使用switch时 在case 后面申请变量会出现 error: a declaration cannot have a label错误
原因:Case statements are only 'labels'. This means the compiler will interpret this as a jump directly to the label.The problem here is one of scope. Your curly brackets define the scope as everything inside the 'switch' statement. This means that you are left with a scope where a jump will be performed further into the code skipping the initialization. The correct way to handle this is to define a scope specific to that case statement and define your variable within it.
解决方法:在case 后面加一个{ }做相应的操作。
本文探讨了在switch语句的case标签后直接声明变量所导致的错误:编译器将其视为标签而非代码的一部分,因此会导致初始化被跳过。文章提供了正确的做法是在case后使用花括号{}
6132

被折叠的 条评论
为什么被折叠?



