A compiler, the compiler error
error: jump to case label [- fpermissive], the error: crosses initialization of 'XXXX' </ code>, to simple combing the related content
I. Problem code
int main()
{
int test = 2;
switch(test)
{
case 1:
int i = 1;
cout << i;
break;
case 2:
cout << i;
break;
default:
cout << "error" << endl;
}
}
//test.cpp: In function 'int main()':
//test.cpp: error: jump to case label [-fpermissive]
// case 2:
// ^
//test.cpp: error: crosses initialization of 'int i'
// int b = 1;
//test.cpp: error: jump to case label [-fpermissive]
// default:
// ^
//test.cpp:11:8: error: crosses initialization of 'int i'
// int b = 1;
As can be seen from the above code, since there is no separate block in switch to qualify the declaration period of variable I, the scope of the variable is the initialization point to the end of switch. In this case, the compiler will report an error because we are not sure whether this variable will be used in other cases and whether it was initialized before it was used. For example, if test has a value of 2 and case 2 is executed directly, an undefined variable will cause an exception. This is a compiler error crosses initialization </ code>.
no matter whether the other branches contain defined variables or not, as long as the variables are not braced in the case.
After inspection, it is found that the compiler will report an error
int main()
{
int test = 2;
switch(test)
{
case 1:
int i = 1;
cout << i;
break;
case 2:
cout << 3;
break;
default:
cout << "error" << endl;
}
}
//test.cpp: In function 'int main()':
//test.cpp: error: jump to case label [-fpermissive]
// case 2:
// ^
//test.cpp: error: crosses initialization of 'int i'
// int i = 1;
//test.cpp: error: jump to case label [-fpermissive]
// default:
// ^
//test.cpp: error: crosses initialization of 'int i'
// int i = 1;
The code of case 1 is enclosed with {}, and the scope of variable I is clearly set to avoid access by other cases
2. The scope of variable I is put outside the switch, and every case in the switch can be accessed
The
switch statement is a kind of goto statement, so goto has the same properties. The following goto statement will not be executed, variable I will definitely be defined, but will report the same error as above. This means that there can be no variables between goto and the tag. Variables must appear before the goto or after the tag.
int main()
{
if(0)
{
goto end;
}
int i = 1;
end:
cout << i;
}
//test.cpp: In function 'int main()':
//test.cpp error: jump to label 'end' [-fpermissive]
// end:
// ^
//test.cpp error: from here [-fpermissive]
// goto end;
// ^
//test.cpp: error: crosses initialization of 'int i'
// int i = 1;
In the above example, it is possible to initialize a variable before the goto tag or after the end tag
Read More:
- C++ [Error] jump to case label [-fpermissive]
- The solution for error: jump to label [-fpermissive] encountered in C
- I encountered a compilation error twice: crosses initialization of…
- C / C + + library function (tower / tower) realizes the conversion of letter case
- error: a label can only be part of a statement and a declaration is not a statement (How to Fix)
- Error: transfer of control bypasses initialization of: variable XXX solution
- Solve QQ startup error: Initialization failure: 0x0000000C
- Implementation of Python switch / case statements
- Python switch / case statement implementation method
- How to distinguish the source channel of router.push jump fast application
- Servlet jump to JSP page 404 error
- Three methods of JavaScript jump to new page
- Click the button to jump to a new window
- In echarts label.formatter Non effectiveness
- 26 English letters in upper and lower case and 0-9 to generate 8-bit random password
- Servlet page Jump error 500
- C / C + + rounding function ceil(), floor ()
- When MyEclipse starts tomcat, the console doesn’t jump out
- C++:error C2228: left of ‘.str’ must have class/struct/union
- AttributeError:‘AxesSubplot’object has no attribute‘bar_label’