警告 C26812:枚举类型未限定范围。比枚举更喜欢枚举 class
Warning C26812: Enum type is unscoped. Prefer enum class over enum
我很困惑为什么我会收到这个警告。我的整个代码中甚至没有枚举?
#include <SFML/Graphics.hpp>
#include <vector>
using sf::RenderWindow;
using sf::VideoMode;
using sf::Event;
using std::vector;
using sf::Vector2f;
using sf::RectangleShape;
using sf::CircleShape;
using sf::Color;
using sf::Keyboard;
int main()
{
RenderWindow window(VideoMode(720, 640), "Shooter game w Projectiles.");
window.setFramerateLimit(60);
CircleShape player(50.f);
player.setFillColor(Color::White);
player.setPosition((window.getSize().x / 2.f) - (player.getRadius()), (window.getSize().y - player.getRadius() * 2) - 10.f);
CircleShape bullet(5.f);
bullet.setFillColor(Color::Red);
vector<CircleShape> playerBullets;
playerBullets.push_back(bullet);
RectangleShape enemy(Vector2f(30.f, 30.f));
enemy.setFillColor(Color::Magenta);
enemy.setPosition(320, 200);
vector<RectangleShape> enemies;
enemies.push_back(enemy);
enemy.setFillColor(Color::Blue);
enemy.setPosition(160, 100);
enemies.push_back(enemy);
while (window.isOpen())
{
Event event;
while (window.pollEvent(event))
{
if (event.type == Event::Closed)
{
window.close();
}
}
if (Keyboard::isKeyPressed(Keyboard::Escape))
{
window.close();
}
// Update
Vector2f playerCenter = Vector2f(player.getPosition().x + player.getRadius(), player.getPosition().y + player.getRadius());
// Clear
window.clear();
// Draw
window.draw(player);
for (size_t i = 0; i < enemies.size(); ++i)
{
window.draw(enemies[i]);
}
for (size_t i = 0; i < playerBullets.size(); ++i)
{
window.draw(playerBullets[i]);
}
// Display
window.display();
}
}
这是我收到的警告:枚举类型 'sf::PrimitiveType' 未限定范围。更喜欢 'enum class' 而不是 'enum' (Enum.3)。
它在第 79 行警告我,这基本上是 main 函数右括号后的新行?
不幸的是,此警告来自头文件 SFML\Graphics.hpp
,唯一的选择是联系 SFML 开发人员并要求他们按照评论部分中@spectras 的建议修复此警告。
我无法实施可以解决警告的解决方案。但是,最好通过将第三方头文件封装在两个 pragma 语句中来禁用来自该第三方头文件的所有警告:
#pragma warning(push, 0)
#include <SFML/Graphics.hpp>
#pragma warning(pop)
感谢@Thrasher 在评论部分为我提供 link。这里是 link:
https://blog.bytellect.com/software-development/c-cplusplus/disabling-warnings-from-legacy-and-third-party-header-files/
我很困惑为什么我会收到这个警告。我的整个代码中甚至没有枚举?
#include <SFML/Graphics.hpp>
#include <vector>
using sf::RenderWindow;
using sf::VideoMode;
using sf::Event;
using std::vector;
using sf::Vector2f;
using sf::RectangleShape;
using sf::CircleShape;
using sf::Color;
using sf::Keyboard;
int main()
{
RenderWindow window(VideoMode(720, 640), "Shooter game w Projectiles.");
window.setFramerateLimit(60);
CircleShape player(50.f);
player.setFillColor(Color::White);
player.setPosition((window.getSize().x / 2.f) - (player.getRadius()), (window.getSize().y - player.getRadius() * 2) - 10.f);
CircleShape bullet(5.f);
bullet.setFillColor(Color::Red);
vector<CircleShape> playerBullets;
playerBullets.push_back(bullet);
RectangleShape enemy(Vector2f(30.f, 30.f));
enemy.setFillColor(Color::Magenta);
enemy.setPosition(320, 200);
vector<RectangleShape> enemies;
enemies.push_back(enemy);
enemy.setFillColor(Color::Blue);
enemy.setPosition(160, 100);
enemies.push_back(enemy);
while (window.isOpen())
{
Event event;
while (window.pollEvent(event))
{
if (event.type == Event::Closed)
{
window.close();
}
}
if (Keyboard::isKeyPressed(Keyboard::Escape))
{
window.close();
}
// Update
Vector2f playerCenter = Vector2f(player.getPosition().x + player.getRadius(), player.getPosition().y + player.getRadius());
// Clear
window.clear();
// Draw
window.draw(player);
for (size_t i = 0; i < enemies.size(); ++i)
{
window.draw(enemies[i]);
}
for (size_t i = 0; i < playerBullets.size(); ++i)
{
window.draw(playerBullets[i]);
}
// Display
window.display();
}
}
这是我收到的警告:枚举类型 'sf::PrimitiveType' 未限定范围。更喜欢 'enum class' 而不是 'enum' (Enum.3)。
它在第 79 行警告我,这基本上是 main 函数右括号后的新行?
不幸的是,此警告来自头文件 SFML\Graphics.hpp
,唯一的选择是联系 SFML 开发人员并要求他们按照评论部分中@spectras 的建议修复此警告。
我无法实施可以解决警告的解决方案。但是,最好通过将第三方头文件封装在两个 pragma 语句中来禁用来自该第三方头文件的所有警告:
#pragma warning(push, 0)
#include <SFML/Graphics.hpp>
#pragma warning(pop)
感谢@Thrasher 在评论部分为我提供 link。这里是 link:
https://blog.bytellect.com/software-development/c-cplusplus/disabling-warnings-from-legacy-and-third-party-header-files/