1

I got a little problem, I get an error:
"C2361: initialization of *identifier* is skipped by 'default' label"

I use an internal class member to set the chosen approach used in my methods.
The methods use this internal member (static int) with a switch to determine which approach was set.
The switch needs an intial value when compiling, so I decided to use a static const int. However, VS is still unhappy, and I can't change the static const int as well. I am pretty sure this ain't a big deal, but it's quite frustrating.

example:

class test{
    public:
    static int Val;
    static void setVal(int val);
    static int doStuff(void);
};

test::setVal(int val){
    Val=val;
}

test::doStuff(){
    switch(Val){
    case 1:
    // do stuff
    case 2:
    // do other stuff
    default:
    // do default
    }
}

Many thanks for any tips or solutions

Serdalis
  • 10,296
  • 2
  • 38
  • 58
Y.S
  • 311
  • 1
  • 10
  • What's "an intern class member"? – Lightness Races in Orbit Sep 29 '15 at 00:30
  • Could it be related to this: http://stackoverflow.com/questions/92396/why-cant-variables-be-declared-in-a-switch-statement ? Maybe your static variable is not the culprit. – Hellmar Becker Sep 29 '15 at 00:37
  • 1
    Your code is missing details. At a guess, the code in `do other stuff` is declaring and initializing a variable. – 1201ProgramAlarm Sep 29 '15 at 00:37
  • @Lightness Races in Orbin: Simple: its my static int Val int this example – Y.S Sep 29 '15 at 00:41
  • @1201ProgramAlarm: do other stuff ist just a change of behaviour, in fact I chose from where my program gets values, and how these are processed, let's say in case 1: I multiply values, and in case 2 I just add the given values. In fact i just don't wanna pass the case value each time a method is called, so I tried this approach with using as case value the static int Val of the class. – Y.S Sep 29 '15 at 00:45
  • That error is telling you that you have code like `case 2: int i = 7; break; default:` where the variable initialized in the second case is still visible and live at the default label. – 1201ProgramAlarm Sep 29 '15 at 00:48
  • @Y.S. I'm asking you to explain your terminology. "intern"? – Lightness Races in Orbit Sep 29 '15 at 00:54
  • @Ligthness Races in Orbin: Yea it's no real intern member since it ain't protected nor private. I used intern since it is a value stored in my class, and so seemed somehow logic. – Y.S Sep 29 '15 at 10:09
  • @1201ProgramAlarm: Thanks a lot, I did actually declare a return value in one case and throw exceptions at the other not yet implemented cases, so you were right on spot. – Y.S Sep 29 '15 at 10:13

1 Answers1

2

There are numerous problems with he code you have posted. But assuming there are no other issues the following code will produce what you are experiencing:

test.h:

class test{
    public:
    static int Val;
    static void setVal(int val);
    static int doStuff(void);
};

test.cpp:

#include "test.h"

int test::Val = 0;

void
test::setVal(int val){
    Val=val;
}

int
test::doStuff(){
    switch(Val){
    case 1:
    // dostuff
        int apples = 1; /* problem line */
        break;
    default:
    // do default
        break;
    }
    return 0;
}

int main(int argc, char **argv)
{
    return 0;
}

Will produce the error:

error C2361: initialization of 'apples' is skipped by 'default' label

The reason for this is because you are trying to initialise a variable within the case statement. Visual Studio is complaining that there is no guarantee that the variable will be initialised because the case statement may be skipped over.

The question linked here gives the reason for this behavior.

But to put it briefly there is a chance that your code will jump to a case statement after the initialisation of your variable and visual studio does not allow such a case since it may lead to undefined behavior.

To quote the standard ( ISO C++ '03 - 6.7/3 ):

A program that jumps from a point where a local variable with automatic storage duration is not in scope to a point where it is in scope is ill-formed unless the variable has POD type (3.9) and is declared without an initializer (8.5)

Community
  • 1
  • 1
Serdalis
  • 10,296
  • 2
  • 38
  • 58
  • In his code, could the problem have been `switch(Val)` since he didn't initialize `test::Val` in his code, but you initialized `test::Val` in your code? – Jonny Henly Sep 29 '15 at 00:57
  • 1
    @JonnyHenly He posted the error he was getting up the top. An un-initialised static gives a different error: `error LNK2001: unresolved external symbol`. My code assumes everything is correct and he is only getting that one error. For the sake of simplicity. – Serdalis Sep 29 '15 at 00:58
  • 1
    Oh gotcha. Shouldn't he have gotten `error LNK2001: ...` before `error C3261`? Not saying anything is wrong with your answer, I'm just curious. Just noticed you edited your last comment adding the assumption. – Jonny Henly Sep 29 '15 at 01:05
  • 1
    @JonnyHenly On my compiler he would get `C2361` before `LNK2001`, so he could still get the `LNK2001` error after he has fixed this one. So you might still be correct. – Serdalis Sep 29 '15 at 01:09
  • @JonnyHenly: Yes, I got both of em, but the initialisiation was what I was actually looking for, simple things lead to huge questionmarks, or so, Thanks a lot guys - the linker error came after c2361... – Y.S Sep 29 '15 at 01:20