简体   繁体   English

警告 C26812:枚举类型“utils::TwilightTypes”未限定范围。 更喜欢“枚举类”而不是“枚举”(Enum.3)

[英]warning C26812: The enum type 'utils::TwilightTypes' is unscoped. Prefer 'enum class' over 'enum' (Enum.3)

I'm developing a library using Visual Studio 2019 and C++ on Windows 10.我正在使用 Visual Studio 2019 和 C++ 在 Windows 10 上开发一个库。

I have the header file "Utils.h" with this declaration:我有带有此声明的 header 文件“Utils.h”:

namespace utils {

    enum TwilightTypes
    {
        Civil,
        Nautical,
        Astronomical
    };

And in the header file "TheSun.h" this class that uses the enum:在 header 文件“TheSun.h”中,这个 class 使用枚举:

#pragma once
#include "Utils.h"

class TheSun
{
private:

public:

    // 50. Twilight
    utils::RiseSetTime Twilight(
        utils::TwilightTypes type,
        utils::dateTime date,
        double longitude,
        char longOrientation,
        double latitude,
        char latOrientation);
};

But, in the implementation of that method, in file "TheSun.cpp" I get the following warning:但是,在该方法的实现中,在文件“TheSun.cpp”中我收到以下警告:

warning C26812: The enum type 'utils::TwilightTypes' is unscoped.警告 C26812:枚举类型“utils::TwilightTypes”未限定范围。 Prefer 'enum class' over 'enum' (Enum.3).更喜欢“枚举类”而不是“枚举”(Enum.3)。

I have changed the declaration of the enum to:我已将枚举的声明更改为:

namespace utils {

    enum class TwilightTypes
    {
        Civil,
        Nautical,
        Astronomical
    };

But I get the same warning.但我得到同样的警告。

How can I remove this warning?如何删除此警告?

I have also move the enum declaration to the header file "TheSun.h", and I get the same warning.我还将枚举声明移至 header 文件“TheSun.h”,我收到了同样的警告。

I have only one enum in this solution.我在这个解决方案中只有一个枚举。

I always clean and rebuild the entire solution.我总是清理并重建整个解决方案。

From Microsoft documentation, here is the explanation of the warning:来自 Microsoft 文档, 这里是警告的解释:

Reason原因

To minimize surprises: traditional enums convert to int too readily.为了尽量减少意外:传统的枚举很容易转换为 int。

Enforcement执法

(Simple) Warn on any non-class enum definition. (简单)警告任何非类enum定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM