Re: compilation error in switch case in c++
Rahul wrote:
> Hi Everyone,
>
> I have the following program,
>
> class A
> {
> int i;
> };
>
> int main()
> {
> switch(1)
> {
> case 1 :
> // {
> A obj;
> printf("case 1\n");
> // }
> break;
> case 2 :
> A obj1;
> printf("case 2\n");
> break;
> }
> }
>
>
> and when i compile, i get an compilation error, saying,
>
> jump to case symbol
> enters scope of non-POD 'A obj'
>
> but if i remove the commented open and close brace, it works just
> fine, could anyone clarify the exact reason of the compiler reporting
> the error,
These are the errors I get with gcc when I compile your code - I made a
few changes i.e. made it a read non POD class.
clcpp_t6.cpp: In function `int main()':
clcpp_t6.cpp:20: error: jump to case label
clcpp_t6.cpp:16: error: crosses initialization of `A obj'
The errors should be self explanatory.
#include <cstdio>
class A
{
public:
int i;
A() : i() {}
};
int main()
{
switch(1)
{
case 1 :
// {
A obj;
std::printf("case 1\n");
// }
break;
case 2 :
A obj1;
std::printf("case 2\n");
break;
}
}
|