c++里面的switch-case问题

来源:互联网 发布:如何看spring源码 编辑:程序博客网 时间:2024/06/05 03:59

./DBOperator.cpp:87: error: jump to case label
./DBOperator.cpp:84: error:   crosses initialization of 'std::string str_timebuf'
./DBOperator.cpp:81: error:   crosses initialization of 'char timebuf [128]'


编译下面的代码会报错:

int main()
{
  int i;
  switch (i)
  {
    case 3:
      int y = 0;
      break;
    default:
      break;
  }
}
 

g++ 编译:
main.cpp: In function `int main()':
main.cpp:11: jump to case label
main.cpp:9: crosses initialization of `int y'

用 gcc 编译:
error: syntax error before "int"

改成这样:
int main()
{
      int i;  
      switch (i)
      {
          case 3: 
              int y ; 
              y = 0;  
              break;  
          default:
              break;  
      }
}
 

gcc 还是没法通过编译。
把 case 部分用 {} 括起来,则都可以通过编译

0 0
原创粉丝点击