本文主要是介绍C++工程,枚举类输出运算符重载,elaborated-type-specifier for a scoped enum must not use the ‘class’ keyword,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
文章目录
- 1,原始代码
- 2,运行没问题,但编译会有警告:elaborated-type-specifier for a scoped enum must not use the ‘class’ keyword
- 3,修改
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;
}
这篇关于C++工程,枚举类输出运算符重载,elaborated-type-specifier for a scoped enum must not use the ‘class’ keyword的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!