why i cant instantiate objects inside a switch-case block

block, c++, instantiation, switch-statement

Solution

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

Problem

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 no(n); break; case 3: cin>>n; n_bin nb(n); break; } ``` on compiling it gives a message "crosses initialisation of n_hexa" for line of n_octa

Original source

Related problems