switch(choice)
    {
        case 1:
            uinstance1.addNewProduct(data);
            break;
        case 2:
            break;
        case 3:
            break;
        case 4:
            break;
        case 5:
            break;
        case 6:
            break;
        case 7:
            uinstance1.listAllProducts(data);
            break;
        case 8:
            break;
        case 9:
            break;
        case 10:

            //name,category,barcode,price,manufacturer,noinstock,soldpermonth,expirydate,discount
            //  Perishable(string,string,string,double,string,int,int);
            Perishable item0("Ferrari","Automobile","9999",2999.99,"Popular",5,0);

            data.addNew(item0);

            break;
        default:
            cout<<"Wrong Choice "<<endl;
            system("pause");
            break;
    }
}

嗨,我已经考虑了很长时间了,似乎无法弄清楚这个问题。

错误C2361:“默认”标签跳过了“item0”的初始化
:参见'item0'的声明

一些帮助,将不胜感激。
谢谢

最佳答案

整个选择块算作一个作用域,如果在该作用域中对变量进行贴花,则需要在每个case语句(所有可能的执行路径)中对其进行初始化。您可以通过在您的案例中创建其他范围来避免此问题来避免此问题(请参见方括号):

switch(choice)
    {
        case 1:
            uinstance1.addNewProduct(data);
            break;
        case 2:
            break;
        case 3:
            break;
        case 4:
            break;
        case 5:
            break;
        case 6:
            break;
        case 7:
            uinstance1.listAllProducts(data);
            break;
        case 8:
            break;
        case 9:
            break;
        case 10:
            {
                //name,category,barcode,price,manufacturer,noinstock,soldpermonth,expirydate,discount
                //  Perishable(string,string,string,double,string,int,int);
                Perishable item0("Ferrari","Automobile","9999",2999.99,"Popular",5,0);

                data.addNew(item0);
            }
            break;
        default:
            cout<<"Wrong Choice "<<endl;
            system("pause");
            break;
    }
}

10-04 18:49