0

I'm getting the 'switch in protected scope error' with this code. It's a vaguely similar problem to some others on this site, however I am am not initialising variables within a case, and adding braces makes no difference.

                switch (switchval){

                case 1:
                    sort(opsThatCanWork.begin(), opsThatCanWork.end(), sortByDurationLow);  //chooses op with smallest duration
                    break;

                case 2:
                    sort(opsThatCanWork.begin(), opsThatCanWork.end(), sortByDurationHigh);  //chooses op with biggest duration
                    break;

                case 3:
                    //chooses op with lowest total job time remaining - need to calculate these
                    sort(opsThatCanWork.begin(), opsThatCanWork.end(), sortByRemainingJobTimeLow);
                    break;

                case 4:
                     //chooses op with lowest total job time remaining - need to calculate these
                    sort(opsThatCanWork.begin(), opsThatCanWork.end(), sortByRemainingJobTimeHigh);
                    break;

                case 5:
                    //this chooses randomly!
                    int randVal = round(randomGen(-0.49999, opsThatCanWork.size()-1+0.49999));
                    opsThatCanWork.at(0) = opsThatCanWork.at(randVal);
                    break;

                case 6:
                    //first in the quene for a machine
                    sort(opsThatCanWork.begin(), opsThatCanWork.end(), sortByStartTimeLow);
                    break;


                case 7:
                    sort(opsThatCanWork.begin(), opsThatCanWork.end(), sortByStartTimeHigh);
                    break;


            }

the error is on the lines that state 'case 6' and 'case 7'

Liam Baron
  • 43
  • 1
  • 9
  • Why have you posted your code with an obnoxious quantity of unnecessary indentation? Couldn't be bothered to remove it? That's okay; I can't be bothered to fix it for you. – Lightness Races in Orbit Feb 04 '15 at 01:29
  • http://stackoverflow.com/a/7334968/560648 – Lightness Races in Orbit Feb 04 '15 at 01:30
  • Because deadlines are looming and that's the indentation it has. It doesn't make any less readable. – Liam Baron Feb 04 '15 at 14:03
  • 1
    Yes, it does make it less readable and, furthermore, it shows a lack of respect for the fact that you're asking for free help. I'm trying to say this as politely as possible, but your deadlines are not our problem and should not be taken into consideration when determining how much time and effort to put into your question. It would only have taken 60 seconds to format it properly. Thanks! – Lightness Races in Orbit Feb 04 '15 at 16:10

3 Answers3

4

A jump (including a jump from switch to a case) is not allowed to bypass a declaration that initializes a variable, except in a limited number of cases.

It is possible to transfer into a block, but not in a way that bypasses declarations with initialization. A program that jumps from a point where a variable with automatic storage duration is not in scope to a point where it is in scope is ill-formed unless the variable has scalar type, class type with a trivial default constructor and a trivial destructor, a cv-qualified version of one of these types, or an array of one of the preceding types and is declared without an initializer (8.5).

So if you have an initializer on the declaration, it is definitely forbidden.

Note that cases 6 and 7 trigger the error, because if you jump to case 5, the label is before the declaration, so there is no problem.

As you already noted in a comment on another answer, putting braces around the initialization of the variable in case 5 fixes the problem.

Brian Bi
  • 111,498
  • 10
  • 176
  • 312
1
                case 5:
                //this chooses randomly!
----------------V variable initialization
                int randVal = round(randomGen(-0.49999, opsThatCanWork.size()-1+0.49999));
                opsThatCanWork.at(0) = opsThatCanWork.at(randVal);
                break;
horns
  • 1,843
  • 1
  • 19
  • 26
  • Ah thanks, braces around case 5 solved it. It threw me off that there was no error there, but on 6 and 7. Any idea why this is? – Liam Baron Feb 04 '15 at 01:12
0

Warning: this isn't a direct answer to your question--but it may well be useful in restructuring the code to be quite a bit better.

Based on your code for case 5, it appears that you really only need to choose the smallest item (according to one of a variety of measurement methods) and put it in opsThatCanWork[0].

If that's the case, you can improve the running speed of the code for all the other cases (other than 5, I mean) by using std::min_element instead of std::sort. std::sort spends quite a bit of time putting all the items in order, but you apparently only really care about finding one element. That's exactly the job for which std::min_element was designed.

At least at first glance, it looks to me like your code for case 5 may well have a bug as well. All the other cases just rearrange the existing elements in opsThatCanWork. The code for case 5, however, overwrites the existing first value with some other random value. If you're only executing this code once, and that array/vector is no longer needed, then that's probably fine--but if you're using the data in that vector again, every time you execute the code for case 5, you lose another data item (and end up with two identical items). Repeat a few thousand times, and your array may contain N copies of a single value instead of N values.

You probably want to fix that by swapping opsThatCanWork[0] with the randomly chosen element instead of overwriting it.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • Thanks very much. Further down the code I'm only using opsThatCanWork[0], and it all gets cleared at the start of the loop. I want to find the first in the vector according to a range of different variables held within the object, which is why I used sort. Can I do the same thing with min_element? – Liam Baron Feb 04 '15 at 10:26
  • @LiamBaron: Yes, that's exactly what `min_element` does. – Jerry Coffin Feb 04 '15 at 15:44