티스토리 뷰

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문 안에 중괄호를 쳐줘야 한다.

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/09   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
글 보관함