C++工程,枚舉類輸出運算符重載,elaborated-type-specifier for a scoped enum must not use the ‘class’ keyword

1,原始代碼

#include <iostream>

enum class TreeType : int {
    PINE = 1,
    CYPRESS = 2,
    WILLOW = 3
};
std::ostream &operator<<(std::ostream &os, const enum class TreeType &rhs) {
    switch(rhs) {
        case TreeType::PINE:
            os << "PINE"; break;
        case TreeType::CYPRESS:
            os << "CYPRESS"; break;
        case TreeType::WILLOW:
            os << "WILLOW"; break;
    }
    return os;
};

int main() {
    TreeType tree_type;
    tree_type = TreeType::WILLOW;
    std::cout << tree_type << std::endl;
    return 0;
}

2,運行沒問題,但編譯會有警告:elaborated-type-specifier for a scoped enum must not use the ‘class’ keyword

在這裏插入圖片描述

3,修改

#include <iostream>

enum class TreeType : int {
    PINE = 1,
    CYPRESS = 2,
    WILLOW = 3
};
//std::ostream &operator<<(std::ostream &os, const enum class TreeType &rhs) {
// 改爲
std::ostream &operator<<(std::ostream &os, const enum TreeType &rhs) {
    switch(rhs) {
        case TreeType::PINE:
            os << "PINE"; break;
        case TreeType::CYPRESS:
            os << "CYPRESS"; break;
        case TreeType::WILLOW:
            os << "WILLOW"; break;
    }
    return os;
};

int main() {
    TreeType tree_type;
    tree_type = TreeType::WILLOW;
    std::cout << tree_type << std::endl;
    return 0;
}

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章