我写了个函数
void CMyDlg::fn(int iNo)
{
switch(iNo)
{

case 1 :
for(int i = 0;i < MAX_NUM ;i++)
{
...
}
break; 

case 2:
for(int i = 0 ;i < MAX_NUM ;i++)
{
...
                            }
break;

case 3:
for(int i = 0 ;i < MAX_NUM ;i++)
{
...
}
break; }

}
但调试时产生错误
error C2360: initialization of 'i' is skipped by 'case' label
不知道是什么原因

解决方案 »

  1.   

    在switch--case语句中是不允许定义变量的,你把变量i的定义写到switch--case外面就可以了。结贴吧,呵呵
      

  2.   

    MSDN的错误提示:Compiler Error C2360
    initialization of 'identifier' is skipped by 'case' labelThe initialization of identifier can be skipped in a switch statement. You cannot jump past a declaration with an initializer unless the declaration is enclosed in a block. (Unless it is declared within a block, the variable is within scope until the end of the switch statement.)Example// C2360.cpp
    void func( void )
    {
       int x;
       switch ( x )
       {
       case 0 :
          int i = 1;       // C2360, skipped by case 1
          { int j = 1; }   // OK, initialized in enclosing block
       case 1 :
          int k = 1;       // OK, initialization not skipped
       }
    }
      

  3.   

    {
    for(int i = 0;i < MAX_NUM ;i++)
    {
    ...
    }
    }
    在for外面加上{ }就可以了
      

  4.   

    for(int i = 0;i < MAX_NUM ;i++)中你应该把int i放在switch外定义
      

  5.   

    void CMyDlg::fn(int iNo)
    {
             int i  = 0;
    switch(iNo)
    {

    case 1 :
    for(i = 0;i < MAX_NUM ;i++)
    {
    ...
    }
    break; 

    case 2:
    for(i = 0 ;i < MAX_NUM ;i++)
    {
    ...
                                }
    break;

    case 3:
    for(i = 0 ;i < MAX_NUM ;i++)
    {
    ...
    }
    break;
                      default:       //最好有,良好的风格
                                break;         
    }

    }