switch case error

Discussion in 'C++' started by gouqizi.lvcha@gmail.com, Sep 30, 2005.

  1. Guest

    Hi, All:

    I remembered that I met a compiler error before that says something
    like "cannot declare variable past a label", who know exectly of what
    the error is?

    For example:

    int c;
    switch (c)
    case 1:

    ....
    break;

    case 2:
    int x;
    break;


    Thanks

    rick
     
    , Sep 30, 2005
    #1
    1. Advertising

  2. Guest

    wrote:
    > Hi, All:
    >
    > I remembered that I met a compiler error before that says something
    > like "cannot declare variable past a label", who know exectly of what
    > the error is?
    >
    > For example:
    >
    > int c;
    > switch (c)
    > case 1:
    >
    > ....
    > break;
    >
    > case 2:

    {
    > int x;

    }
    > break;


    Introduce a new scope. /david
     
    , Sep 30, 2005
    #2
    1. Advertising

  3. wrote:
    > I remembered that I met a compiler error before that says something
    > like "cannot declare variable past a label", who know exectly of what
    > the error is?
    >
    > For example:
    >
    > int c;


    You forgot to initialise it or to assign anything to it...

    > switch (c)


    {

    > case 1:
    >
    > ....
    > break;
    >
    > case 2:
    > int x;
    > break;


    case 3:
    ...

    and at this point, should 'c' be 3, the declaration of 'x' is
    bypassed by this case label. While 'x' should be visible, its
    declaration/definition isn't "executed". That's what is not
    allowed. To overcome it you need to surround the contents of
    the 'case 2' block with curly braces thus making it a compound
    statement:

    case 2:
    {
    int x;
    ...
    break;
    }

    V
     
    Victor Bazarov, Sep 30, 2005
    #3
  4. Guest

    Victor,

    "Some declaration is bypassed by some execution path" will cause what
    problem? It is quite strange.

    Rick
     
    , Sep 30, 2005
    #4
  5. Dan Cernat Guest

    wrote:
    > Victor,
    >
    > "Some declaration is bypassed by some execution path" will cause what
    > problem? It is quite strange.
    >
    > Rick


    That is why that is an error. The problem isn't the fact that "some
    declaration is bypassed by some execution path". The problem is that
    the variable whos declaration was bypassed is visible down the
    execution path.

    #include <iostream>

    int main()
    {
    int c = 0;

    std::cin >> c;

    switch(c)
    {
    case 1:
    int x = 0;
    ++x;
    break;
    case 2:
    ++x; // <-- the problem is here
    }

    return 0;
    }

    the spot I marked is part of the scope of x. the problem is that if c
    == 2, the declaration of x is never executed.

    HTH
    dan
     
    Dan Cernat, Sep 30, 2005
    #5
  6. Guest

    Thanks. I got it.

    Rick.
     
    , Oct 1, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. gane kol

    switch case with enum

    gane kol, May 6, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    7,341
    Kevin Spencer
    May 6, 2005
  2. Brian J. Sayatovic

    Why constant ints in switch case expressions?

    Brian J. Sayatovic, Jul 2, 2003, in forum: Java
    Replies:
    22
    Views:
    21,929
    Dale King
    Jul 9, 2003
  3. Rahul
    Replies:
    6
    Views:
    2,863
    James Kanze
    Feb 8, 2008
  4. Lakshmi Narayanan.R

    "Case 1 To 5" in Select case, giving Error!!. Why?

    Lakshmi Narayanan.R, Mar 2, 2005, in forum: ASP General
    Replies:
    10
    Views:
    372
    Bob Barrows [MVP]
    Mar 4, 2005
  5. Switch Within A Switch

    , Apr 22, 2006, in forum: Javascript
    Replies:
    7
    Views:
    130
    Lasse Reichstein Nielsen
    Apr 22, 2006
Loading...

Share This Page