比如∶switch (a)
{
   case 1:
       CString str="ABCDE";  //这句编译有错误。请问为什么?
       break;
   case 2:
       break;
   ....
}

解决方案 »

  1.   

    gz我也遇过,改成CString str;
    str="abcde";就好使了.不知为什么.
      

  2.   

    gz我也遇过,改成CString str;
    str="abcde";就好使了.不知为什么.
      

  3.   

    switch (a)
    {
       case 1:
           {
              CString str="ABCDE";  //这句编译有错误。请问为什么?
           }
           break;
       case 2:
           break;
       ....
    }加上花括号就没事了。这是因为如果a为2的话变量初始化语句将不被执行,
      

  4.   

    e:\rocklee's files\New Folder\myproject\test\test.cpp(13) : error C2360: initialization of 'str' is skipped by 'case' label
            e:\rocklee's files\New Folder\myproject\test\test.cpp(11) : see declaration of 'str'
    e:\rocklee's files\New Folder\myproject\test\test.cpp(15) : error C2361: initialization of 'str' is skipped by 'default' label
            e:\rocklee's files\New Folder\myproject\test\test.cpp(11) : see declaration of 'str'因为你对str的定义可能会被跳过,是以后的错误出现错误吧
      

  5.   

    你这样想,如果 (a == 2)= TRUE ,那么 str 肯定没有。
    complier怎么分配空间?
      

  6.   

    sorry ,使以后的语句发生错误:)
      

  7.   

    switch 是一个单独的程序段。而case(不加{})就不是一个
    单独的程序段。看这个例子:
    int j=1;
    switch(j) {
    case 1:int b;break;
    case 2:b=1;break;
    }
    可以编译。MSDN有下面的说明:
    Compiler Error C2360
    initialization of 'identifier' is skipped by 'case' labelThe specified identifier initialization can be skipped in a switch statement.It is illegal to jump past a declaration with an initializer unless the declaration is enclosed in a block.The scope of the initialized variable lasts until the end of the switch statement unless it is declared in an enclosed block within the switch statement.The following is an example of this error:void func( void )
    {
       int x;
       switch ( x )
       {
       case 0 :
          int i = 1;       // error, skipped by case 1  
          { int j = 1; }   // OK, initialized in enclosing block
       case 1 :
          int k = 1;       // OK, initialization not skipped
       }
    }
      

  8.   

    对于 switch (k)
    {
     case 1:
          int i;
     case 2: 
          i=0;
    }
    如果按照whjpn (常盘平) 的想法,那么会有问题发生:
      如果 k=1,则没什么
      如果 k=2, 则i是谁定义的?这个问题怎么解决?所以对上述问题的修正办法是:
      1.在switch外声明变量,这样case 2就不会有错误了
      2.在switch中声明局部变量,局部变量的标志是用{}
        即:case 1:
               { int i;
                }
        但是case 2就不能使用i了
    这样讲明白了吗?如果明白,记得给分阿!
    呵呵
    编程快乐
      

  9.   

    我也遇到过。
    你这样定义什么问题都没有,就有一个错误。
    加括号!把case1后面的处理语句到break之前用一对括号括起来就没有问题了。
    原因我也不清楚,但加了括号就没问题。