c ++警告:枚举值未在开关[-Wswitch]中处理 [英] c++ warning: enumeration value not handled in switch [-Wswitch]

查看:320
本文介绍了c ++警告:枚举值未在开关[-Wswitch]中处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在没有警告的情况下编译以下代码:

I am trying to compile following code without warnings:

    while (window.pollEvent(event))
    {
        switch (event.type) {
            case sf::Event::Closed:
                window.close(); break;
            case sf::Event::KeyPressed:
                if(event.key.code == sf::Keyboard::Escape )
                    window.close();
                if( sf::Keyboard::isKeyPressed( sf::Keyboard::Space ) )
                    particleSystem.fuel( 200/* * window.getFrameTime() */);
                if( sf::Keyboard::isKeyPressed( sf::Keyboard::A ) )
                    particleSystem.setPosition( --xpos, ypos );
                if( sf::Keyboard::isKeyPressed( sf::Keyboard::D ) )
                    particleSystem.setPosition( ++xpos, ypos );
                if( sf::Keyboard::isKeyPressed( sf::Keyboard::W ) )
                    particleSystem.setPosition( xpos, --ypos );
                if( sf::Keyboard::isKeyPressed( sf::Keyboard::S ) )
                    particleSystem.setPosition( xpos, ++ypos );
                if( sf::Keyboard::isKeyPressed( sf::Keyboard::Left ) )
                    particleSystem.setGravity( --xgrv * 0.1f, ygrv * 0.1f);
                if( sf::Keyboard::isKeyPressed( sf::Keyboard::Right ) )
                    particleSystem.setGravity( ++xgrv * 0.1f, ygrv * 0.1f );
                if( sf::Keyboard::isKeyPressed( sf::Keyboard::Up ) )
                    particleSystem.setGravity( xgrv * 0.1f, --ygrv * 0.1f );
                if( sf::Keyboard::isKeyPressed( sf::Keyboard::Down ) )
                    particleSystem.setGravity( xgrv * 0.1f, ++ygrv * 0.1f );
                if( sf::Keyboard::isKeyPressed( sf::Keyboard::G ) )
                    particleSystem.setGravity( 0.0f, 0.0f );
                if( sf::Keyboard::isKeyPressed( sf::Keyboard::P ) )
                    particleSystem.setPosition( 320.0f, 240.0f );
                break;
    }

但是,我收到很多警告:

however, I am getting a lot of warnings:

/home/bluszcz/private/repo/deerportal/game.cpp:444: warning: enumeration value 'LostFocus' not handled in switch [-Wswitch]

这对我来说不是问题,因为我不需要处理所有类型的

Which in my it is not an issue, since I am don't need handling all types of the events.

添加

default:
    break;

我的代码删除了警告,但这是解决此问题的最佳方法吗?

to my code removes the warnings, however is it a best way to solve this issue?

推荐答案

要明确



这取决于您要实现的目标。控制规则是

Be explicit

It depends on what you are trying to achieve. The governing rule is


最好明确一些。

It is better to be explicit.

省略案例只会使您看起来有些忘记。明确地向代码的后续读者保证,您打算对某些事件不采取任何措施。

Omitting the cases simply makes it look like you forgot some. Being explicit assures subsequent readers of your code that you intended to do nothing for certain events.

鉴于此,您有几个选项:

In light of that, you have a couple of options:

default:
  break;

这可以取消警告,并清楚表明您不打算处理其他事件类型

This suppresses the warning, and makes it clear that you don't intend to handle the other event types here.

列出每个事件类型,后跟一个 break
这也是很明显的,并且具有额外的好处,如果您添加了事件类型,编译器将再次警告您开关不完整。当您有许多switch语句时,这可能很有价值,当添加枚举值时,其中一些语句需要进行修改以做新的事情。

List each event type, followed by a break. This is also explicit, and has the added bonus that, should you ever add an event type, the compiler will once again warn you that your switch is incomplete. This can be valuable when you have many switch statements, some of which need to be modified to do something new when an enum value is added.

我不建议在此处使用一系列 if 语句。 开关更清晰,减少了打字量,并且(如您所见)对于遗漏的情况可以产生更好的编译器警告。

I would not recommend using a series of if statements here. A switch is clearer, reduces the amount of typing, and (as you've seen) can produce better compiler warnings for cases you omitted.

这篇关于c ++警告:枚举值未在开关[-Wswitch]中处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆