error C2361: initialization of 'found' is skipped by 'default' label
本问题已经有最佳答案,请猛点这里访问。
Possible Duplicate:
Why can't variables be declared in a switch statement?
我在下面的代码中有一个奇怪的错误:
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 | 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编译器表示:
1 2 | 1>c:\users\daviti\documents\visual studio 2010\projects\2-3-4\2-3-4\2-3-4.cpp(317): error C2361: initialization of 'found' is skipped by 'default' label 1> c:\users\daviti\documents\visual studio 2010\projects\2-3-4\2-3-4\2-3-4.cpp(308) : see declaration of 'found' |
我认为我已经正确地编写了break和default语句,那么错误在哪里呢?
您需要将您的
1 2 3 4 5 6 7 8 9 10 11 | 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; } |
或者将
考虑到您的
1 2 | int found; found = thetree->find(value); |
(我完整地提到这个。这不是我想要的解决办法重新评论)
需要在花括号内声明
1 2 3 4 5 6 | case 'f' : { ... int found=thetree->find(value); ... } |