我写了个函数
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
不知道是什么原因
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
不知道是什么原因
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
}
}
for(int i = 0;i < MAX_NUM ;i++)
{
...
}
}
在for外面加上{ }就可以了
{
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;
}
}