“name”初始化被“case”标签跳过
在编写Switch语句时,出现该error,查阅资料总结得出:
可以在
switch
语句中跳过identifier
的初始化。在块中声明,否则变量在
switch
语句结束之前都在范围内。
以下示例生成 C2360:
// C2360.cpp
int main() {
int x = 0;
switch ( x ) {
case 0 :
string name;
{ int j = 1; }
case 1 : // C2360
int k = 1;
}
}
可以把定义的变量写到大括号里,以此生成一个代码块,可以解决该问题:
// C2360b.cpp
int main() {
int x = 0;
switch ( x ) {
case 0 :
{
string name;
int i = 1;
}
case 1 :
int k = 1;
}
}