c++ warning: enumeration value not handled in switch [-Wswitch](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
最好是明确的.
省略案例只会让您看起来好像忘记了一些案例.明确表明您的代码的后续读者打算对某些事件不做任何事情.
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
不完整.当您有许多 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
语句.switch
更清晰,减少了输入量,并且(如您所见)可以为您省略的情况产生更好的编译器警告.
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] 中处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:c++ 警告:枚举值未在开关 [-Wswitch] 中处理


基础教程推荐
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01