Reprint http://hi.baidu.com/hins_pan/item/61ff3d8b2e072e52e63d1950
Thanks to the original author O (∩_∩) o~ the same problem when writing code, my Code does not post a similar
Code:
int main ()
{
int a = 0;
Switch (a)
{
Case 0:int B = 0;break;
Case 1:break;
Default:break;
}
return 0;
}
Compiler prompt Error:
testswitch.cpp:In function ' int main () ':
Testswitch.cpp:9: Error:jump to Case label
Testswitch.cpp:8: error:crosses initialization of ' int B '
Testswitch.cpp:10:error:jump to Case label
Testswitch.cpp:8: error:crosses initialization of ' int B '
With this hint, you are likely to have a local variable defined in one of the case tags, followed by the additional case tag or the default statement. For example, the shaping variable b here.
See Compiler hints for information cross initialization of int B, what does that mean, skipping the initialization of the variable, think about it, yes, we've defined variable B in case 0,
In this program, until the "}" closing curly brace of switch is encountered, the scope of B is terminated, meaning that variable B is still accessible in case 1 and the default branch. Consider such a situation,
If the switch matches Case 1 so that the code for case 0 is skipped, then B is undefined, and if you access B in the code in case 1, the program crashes. If no one matches, execute the default
There will be the same danger.
Knowing the cause of the mistake, it's easy to solve.
1, enclose the code in case 0 marked in {} so that the scope of B is within this curly brace. cannot be accessed in other case tags.
2. Place the variable b outside the switch definition.
Error:crosses Initialization of