initialization of xxx is skipped by xxx

来源:互联网 发布:用手机安装plc编程软件 编辑:程序博客网 时间:2024/05/20 05:53
使用case或goto语句时,有时会碰到如下提示:
vc2008:initialization of xxx is skipped by xxx
gcc:crosses initialization of xxx

以case为例:
int main( void ){    int a = 2;    switch (a)    {    case 0:        int b = 0;        break;    case 1:        cout << a << endl;break;    default:        break;    }    return 0;}

codeblocks(gcc) 编译提示:
In function `int main()':
error: jump to case label
error:   crosses initialization of `int b'
error: jump to case label|
error:   crosses initialization of `int b'
warning: unused variable 'b'
=== Build finished: 4 errors, 1 warnings ===

codeblocks(vc2008) 编译提示:
error C2360: initialization of 'b' is skipped by 'case' label
error C2361: initialization of 'b' is skipped by 'default' label
=== Build finished: 2 errors, 0 warnings ===

问题出在 b 的作用域,b 只在"case 0"的情况下进行了初始化,如果是其他语句就会被跳过去,

改变 b 的作用域就可以了,一般使用{}就可以了

   ...   case 0:   {        int b = 0;   }   break;   ...


原创粉丝点击