C++ 错误 C2361:“发现”的初始化被“默认”标签跳过

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10381144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 13:59:39  来源:igfitidea点击:

error C2361: initialization of 'found' is skipped by 'default' label

c++compiler-errorsswitch-statement

提问by dato datuashvili

Possible Duplicate:
Why can't variables be declared in a switch statement?

可能的重复:
为什么不能在 switch 语句中声明变量?

I have a strange error in my code below:

我在下面的代码中有一个奇怪的错误:

char choice=Getchar();
switch(choice)
{
case 's':
    cout<<" display tree ";
    thetree->displaytree();
    break;

case 'i':
    cout<<"  enter value to insert "<<endl;
    cin>>value;
    thetree->insert(value);
    break;
case 'f' :
    cout<< "enter value to find ";
    cin>>value;
    int found=thetree->find(value);
    if(found!=-1)
        cout<<" found  =  "<<value<<endl;
        else
            cout<< " not found " <<value <<endl;
        break;
default:
    cout <<" invalid entry "<<endl;;
    }

Visual Studio 2010 compiler says that:

Visual Studio 2010 编译器说:

1>c:\users\daviti\documents\visual studio 2010\projects-3-4-3-4-3-4.cpp(317): error C2361: initialization of 'found' is skipped by 'default' label
1>          c:\users\daviti\documents\visual studio 2010\projects-3-4-3-4-3-4.cpp(308) : see declaration of 'found'

I think that I have correctly written break and default statements, so where is the error?

我认为我已经正确编写了 break 和 default 语句,那么错误在哪里?

回答by Component 10

You need to either enclose your case 'f':with a scoped brace:

您需要case 'f':用范围大括号括起来:

case 'f' :
{  
    cout<< "enter value to find ";
    cin>>value;
    int found=thetree->find(value);
    if(found!=-1)
        cout<<" found  =  "<<value<<endl;
    else
        cout<< " not found " <<value <<endl;
    break;
}

or place the declaration of foundoutside of the switch

或将声明found放在switch

回答by James Kanze

The semantics of a switchare those of a goto: cases don't introduce a new scope. So foundis accessible in your default:case (although you don't actually access it). Jumping over a non-trivial initialization is illegal, so your code becomes illegal.

的语义switch是那些的gotocase■不要引入一个新的范围。所以found在你的default:情况下是可以访问的(尽管你实际上没有访问它)。跳过非平凡的初始化是非法的,因此您的代码变得非法。

Given the complexity of your case 'f':, the best solution is probably to factor it out into a separate function. Failing that, you can put the entire case in {...}, creating a separate scope, or forgo the initialization, writing:

鉴于您的 的复杂性case 'f':,最好的解决方案可能是将其分解为一个单独的函数。如果失败,您可以将整个案例放在 中{...},创建一个单独的范围,或者放弃初始化,编写:

int found;
found = thetree->find(value);

(I mention this for completeness. It is notthe solution I would recomment.)

(我提到这一点是为了完整性。这不是我推荐的解决方案。)

回答by iammilind

You need to declare the internal variables of switch's casewithin curly braces. i.e.

您需要在花括号内声明switch's的内部变量case。IE

case 'f' :
{
    ...
    int found=thetree->find(value);
    ...
}