why i cant instantiate objects inside a switch-case block

前端 未结 3 811
无人及你
无人及你 2021-02-15 12:36

my code has 3 classes n_hexa,n_octa,n_bin. The code is here

switch(choice)
{
case 1: cin>>n; 
 n_hexa nx(n);
        break;
case 2: cin>>n; 
 n_octa          


        
3条回答
  •  时光取名叫无心
    2021-02-15 13:05

    If you want to have temporary objects inside a case, you'll need to scope them properly.

    switch(choice)
    {
        case 1:
        {
             cin>>n; 
             n_hexa nx(n);
             break;
        }
        case 2:
        {
             cin>>n; 
             n_octa no(n);
             break;
        }
        case 3:
        {
             cin>>n;
             n_bin nb(n);
             break;
        }
    }
    

提交回复
热议问题