初始化操作由 case 标签跳过

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cloud323/article/details/81567041

一,初始化操作由 case 标签跳过

如果要在case里面定义变量,需要用括号括起来{},不然会出错,例如:

#include <iostream>
using namespace std;

void main()
{
    int val = 2;
    switch(val)
    {
    case 1:
        int x = 20;
        break;
    case 2:
        break;
    default:
    }
}

错误信息:

main.cpp(15): error C2360: “x”的初始化操作由“case”标签跳过。

猜你喜欢

转载自blog.csdn.net/cloud323/article/details/81567041