-1

Before you mark as duplicate, I have read the post here and cannot figure out how this relates to my code.

I am getting some compilation errors:

controller.cc: In member function ‘void Controller::start()’:
controller.cc:50:9: error: jump to case label [-fpermissive]
controller.cc:44:17: error:   crosses initialization of ‘uint32_t c’

Here is my code:

void Controller::start(){

    int curPosX = view->getWidth() / 2;
    int curPosY = view->getHeight() / 2;
    uint32_t color = 0xFFFFFF;

    DeviceHandler player = DeviceHandler("/dev/input/js0", false);
    player.flush();

    while(1){

        button_event_t press;
        bool ret = player.poll(&press);

        if(ret && press.type == PRESS){
            switch(press.name){

                case DPAD_UP:
                    curPosY += 1;
                    break;
                case DPAD_DOWN:
                    curPosY -= 1;
                    break;
                case DPAD_RIGHT:
                    curPosX += 1;
                    break;
                case DPAD_LEFT:
                    curPosX -= 1;
                    break;
                case BUTTON_LB:
                    view->clear();
                    break;
                case BUTTON_RB:
                    uint32_t c = rand() & 0xff;
                    c |= (rand() & 0xff) << 8;
                    c |= (rand() & 0xff) << 16;
                    c |= (rand() & 0xff) << 24;
                    color = c;
                    break;
                case BUTTON_A:
                    color = 0x000000;
                    break;
            }

        }
        //color = 0x000000;
        view->drawPixel(curPosY,curPosX, color);
        usleep(500);
    }
}

Now the problem lies in case BUTTON_RB: and case BUTTON_A: statements. When I remove case BUTTON_A: it compiles fine. The post I linked above has stated that this error only occurs when you use an initialized variable in another case statement other than where it was defined. This is not the case here.

The only variable I define is uint32_t c which I do not use elsewhere. I have tried turning these statements into explicit blocks as suggested in other post and it compiles but does not seem to catch correctly.

Community
  • 1
  • 1
Benjamin
  • 280
  • 1
  • 5
  • 21
  • 1
    `The post I linked above has stated that this error only occurs when you use an initialized variable in another case statement other than where it was defined.` No it does not. – cpplearner Jul 31 '16 at 17:46
  • Indeed it does not say that. This is already answered more than well enough on that other question. –  Jul 31 '16 at 17:49
  • As for "I have tried turning these statements into explicit blocks as suggested in other post and it compiles but does not seem to catch correctly." -- I have trouble figuring out what you mean by this. There is no exception handling, so you're not using the standard meaning of "catch". If you mean it does not jump to the correct case label, then that's wrong, it will. If you mean something else, then please be clearer what you mean. –  Jul 31 '16 at 17:51
  • I voted up this question. The C syntax is not intuitive that a case label can not have a declaration without the {}. While it is a duplicate question, it is easy to see how the answers in the other question leave out enough information to leave the reader in the dark. – VectorVortec Dec 18 '18 at 20:14

1 Answers1

1

The fact that you only use it within the case BUTTON_RB doesn't change the fact that it has got local scope. Put it within its own scope, or better yet, its own function.

Also, rand() is really deprecated, and you'll get away much easier if you just used one of the std 32 bit random generators.

Marcus Müller
  • 34,677
  • 4
  • 53
  • 94