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
Related problems
- Can I declare variables inside an Objective-C switch statement?
- Declaring variables inside a switch statement
- Compile Error with: switch, "expected expression before"
- Why can’t variables be declared in a switch statement?
- create an object in switch-case
- Odd compiler error when using Obj-C objects in a switch statement