Why I can't declare a std::string type in a switch/case box?
Here is the code.
Code:
int main(int argc, char* argv[]){
int i=2;
switch (i)
{
case 0:
cout<<0<<endl;
break;
case 1:
string test="";
cout<<1<<endl;
break;
case 2:
cout<<2<<endl;
break;
default:
break;
}
return 0;
}
as you can see, when declare a string test it reports errors
Quote:
main.cc: In function `int main(int, char**)':
main.cc:24: error: jump to case label
main.cc:21: error: crosses initialization of `std::string test'
main.cc:27: error: jump to case label
main.cc:21: error: crosses initialization of `std::string test'
main.cc:24: warning: destructor needed for `std::string test'
main.cc:24: warning: where case label appears here
main.cc:24: warning: (enclose actions of previous case statements requiring
destructors in their own scope.)
main.cc:27: warning: destructor needed for `std::string test'
main.cc:27: warning: where case label appears here
make: *** [main.o] Error 1
I don't understand the errors, what is "jump to case label" ?
why I "crosses initialization of `std::string test'" ?