sfml- warning

I star to learn sfml(C::B, win7).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include<SFML/Window.hpp>
#include <SFML/OpenGL.hpp>
#include<iostream>
using namespace std;

int main()
{
    // Create the main window
sf::Window window;
window.create(sf::VideoMode(800,600),"opengl",sf::Style::Default);
    	// Start the game loop
	window.setVerticalSyncEnabled(true); // call it once, after creating the window

    while (window.isOpen())
    {
        // Process events
        sf::Event event;
        while (window.pollEvent(event))
        {
            // Close window : exit
            switch(event.type){
            case sf::Event::Closed:  window.close();break;
            case sf::Event::KeyReleased:
                    switch(event.key.code){
                    case sf::Keyboard::Escape: cout<<"escape press"<<endl;break;
                    case sf::Keyboard::RShift : cout<<"rshift press"<<endl;break;
                    case sf::Keyboard::LShift: cout<<"lshift press"<<endl;break;

                    }
            }


            }

        
        window.display();
    }

    return EXIT_SUCCESS;
}


warning on line 24:- warning: enumeration value 'A' not handled in switch [-Wswitch]|
|warning: enumeration value 'B' not handled in switch [-Wswitch]|
................................................
warning: enumeration value 'D' not handled in switch [-Wswitch]|
_______________________________________________________________________
similarly line 21:
warning: enumeration value 'Num0' not handled in switch [-Wswitch]|
............................
warning: enumeration value 'Num9' not handled in switch [-Wswitch]|
enumeration value 'LControl' not handled in switch [-Wswitch]|

similarly
'LAlt','LSystem' 'RControl'

warning: enumeration value 'Space' not handled in switch [-Wswitch]|
warning: enumeration value 'Return' not handled in switch [-Wswitch]|

how to handle this.
Last edited on
What's the problem?
May I suggest using if-else rather than a switch as the warnings seems to suggest that the switch will not work with the enums. Also, if you really want to condense your code that much it is entirely possible to do it in just as short a number of lines with repeated ifs and elses.
May I suggest using if-else rather than a switch

Nah, just add a default case in your switch/case statement


1
2
3
4
5
6
7
8
9
10
11
12
            switch(event.type){
            case sf::Event::Closed:  window.close();break;
            case sf::Event::KeyReleased:
                    switch(event.key.code){
                    case sf::Keyboard::Escape: cout<<"escape press"<<endl;break;
                    case sf::Keyboard::RShift : cout<<"rshift press"<<endl;break;
                    case sf::Keyboard::LShift: cout<<"lshift press"<<endl;break;

                    default: break;
                    }
            default: break;
            }
Last edited on
Firstly, these are "only" warnings. This is legal C++, and will compile and run. A warning means that the compiler has identified something unusual, that might lead to problems, or lead to the program doing something that you don't expect, or else something that might indicate you've made a mistake.

While you can ignore warnings, it's generally a good idea to understand them, examine the code that's causing them, and fix them where possible.

I'm not familiar with C:B, but a quick Google search (which you could have done yourself, trivially) tells me that the compiler is warning you that there are some possible values of the enum that aren't handled by cases in your switch statement. So, for example, event.type can take values other than Closed or KeyReleased, and if it does have one of those other values, then it won't trigger any of the cases in your switch problem.

This is perfectly legal, but it's the sort of thing that can sometimes mean that the developer has accidentally forgotten to write a case handling a possible value, so the compiler writers thought it would be helpful to warn you when it happens.

Some developers would say that if this is what you genuinely want to happen, then it would be good style to add a default block that does nothing, e.g.

20
21
22
23
24
25
26
27
28
29
30
31
            // Close window : exit
            switch(event.type){
            case sf::Event::Closed:  window.close();break;
            case sf::Event::KeyReleased:
                    switch(event.key.code){
                            // ...
                    }
                    break;
            }
           default:  
                    // Do nothing for other event types
                    break;


Your code then documents that the missing case statements are not accidental, but that you intended it to work this way.

Last edited on
problem is switch statement. but why?
@Gamer2015 (394)
why warning? how to make my code fresh
Last edited on
Um... did you miss my post? I explained what the problem was, and one way to fix it.
Last edited on
thx every one.
add default work fine.
You're welcome! Glad we could help.
Topic archived. No new replies allowed.