Error:crosses Initialization of

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.