Algorithm/Debugging Notes
[C++] Cannot jump from switch statement to this case label 오류 해결
ylog
2022. 1. 3. 19:36
Xcode로 코딩하다 난데없이 나타난 Cannot jump from switch statement to this case label 오류.
https://en.cppreference.com/w/cpp/language/switch 를 통해 알아보니
라고 한다.
즉 위 case: 문에서 init_statement를 작성한 경우, 아래 case: 문에서 x에대한 초기화 없이 x를 사용하기 때문에 오류가 난다고 이해했다. (잘못되었다면 지적 부탁드립니다.)
그래서
switch(1) {
case 1: int x = 0; // initialization
std::cout << x << '\n';
break;
default: // compilation error: jump to default: would enter the scope of 'x'
// without initializing it
std::cout << "default\n";
break;
}
가 아닌
switch(1) {
case 1: { int x = 0;
std::cout << x << '\n';
break;
} // scope of 'x' ends here
default: std::cout << "default\n"; // no error
break;
}
과 같이 case문 안에 중괄호를 쳐줘야 한다.