switch case error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gouqizi.lvcha@gmail.com

    switch case error

    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

  • davidrubin@warpmail.net

    #2
    Re: switch case error


    gouqizi.lvcha@g mail.com wrote:[color=blue]
    > 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:[/color]
    {[color=blue]
    > int x;[/color]
    }[color=blue]
    > break;[/color]

    Introduce a new scope. /david

    Comment

    • Victor Bazarov

      #3
      Re: switch case error

      gouqizi.lvcha@g mail.com wrote:[color=blue]
      > 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;[/color]

      You forgot to initialise it or to assign anything to it...
      [color=blue]
      > switch (c)[/color]

      {
      [color=blue]
      > case 1:
      >
      > ....
      > break;
      >
      > case 2:
      > int x;
      > break;[/color]

      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


      Comment

      • zealot
        New Member
        • Sep 2005
        • 8

        #4
        Originally posted by gouqizi.lvcha@g mail.com
        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

        I think you may need to put the curly braces to define the scope of the switch statement.

        Comment

        • gouqizi.lvcha@gmail.com

          #5
          Re: switch case error

          Victor,

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

          Rick

          Comment

          • Dan Cernat

            #6
            Re: switch case error


            gouqizi.lvcha@g mail.com wrote:[color=blue]
            > Victor,
            >
            > "Some declaration is bypassed by some execution path" will cause what
            > problem? It is quite strange.
            >
            > Rick[/color]

            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

            Comment

            • gouqizi.lvcha@gmail.com

              #7
              Re: switch case error

              Thanks. I got it.

              Rick.

              Comment

              Working...