Reputation: 43
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'
Upvotes: 0
Views: 3332
Reputation: 490623
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.
Upvotes: 0
Reputation: 119517
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.
Upvotes: 4
Reputation: 1933
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;
Upvotes: 1