c++类切换大小写

发布于 01-07 04:56 字数 1031 浏览 2 评论 0原文

    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:“default”标签跳过“item0”的初始化 :请参阅“item0”的声明

如果有一些帮助,我们将不胜感激。 谢谢

    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;
    }
}

Hi ,i have been thinking about this error for quite some time and cant seem to figure out the issue.

error C2361: initialization of 'item0' is skipped by 'default' label
: see declaration of 'item0'

Some help would be appreciated.
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

蝶…霜飞2025-01-14 04:56:09

整个 select 块算作一个作用域,如果您在该作用域中声明一个变量,则需要在每个 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;
    }
}

The whole select block counts as one scope, if you decalare a variable in that scope you need to initialize it in every case statement (every possible execution path). You can avoid it by creating a additional scope in your case to avoid the problem (see the brackets):

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;
    }
}
往日2025-01-14 04:56:09

MSDN 解释了错误 C2361 恰当地:

在 switch 语句中可以跳过标识符的初始化。除非声明包含在块中,否则您无法跳过带有初始值设定项的声明。 (除非它是在块中声明的,否则该变量在 switch 语句结束之前都在范围内。)

始终注意错误号,它们提供了有关错误原因的重要信息。

你忘记了其中一个案例中的牙套。

   case 10:
    {  
   ^^^
       Perishable item0;
       data.addNew(item0);

       break;
    }  
   ^^^ 

MSDN explains the error C2361 aptly:

The 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.)

Always pay attention to the error numbers they provide vital information about why the error.

You forgot the braces in one of the cases.

   case 10:
    {  
   ^^^
       Perishable item0;
       data.addNew(item0);

       break;
    }  
   ^^^ 
天涯沦落人2025-01-14 04:56:09

您的标签正在跨越初始化,这是非法的。很确定将 default 移动到顶部应该可以修复它。您还可以在相关代码周围添加代码块 { }。如果仍然有问题,请将物体移到开关块之外。

Your label is crossing initialization which is illegal. Pretty sure moving default to the top should fix it. You can also add code blocks { } around your relevant code. If you still have problems then move your object outside of the switch block.

烟雨凡馨2025-01-14 04:56:09

如果没有明确定义范围,则无法在 case 语句内创建变量。

还有另一个讨论:case 语句内的变量

You can't create variables inside case statements if you don't define explicity the scope.

There is another discussion about that: Variables inside case statement

梦年海沫深2025-01-14 04:56:09
case 10:
{ // <<-- This gives explicit scope for the stack variable and let's you get rid of the error
  Perishable item0;
  // ...
}
break;
case 10:
{ // <<-- This gives explicit scope for the stack variable and let's you get rid of the error
  Perishable item0;
  // ...
}
break;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文