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 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;
}