C 操作员在枚举类中的过载
c++ operator overload in an enum class
因此,我正在使用C 编写游戏,在我的教程状态下,我有不同的步骤,用户可以通过,解释了游戏的工作原理。我想增加执行某个操作后用户处于哪个步骤。(鼠标单击)。我尝试使++
运算符超载,但我遇到了一个错误说binary '++': 'STEPS' does not define this operator or a conversion to a type acceptable to the predefined operator
。我正在使用Visual Studio,它的错误代码C2676
。
我的枚举课程设置如下:
enum class STEPS
{
ONE,
TWO,
END_OF_LIST
};
STEPS& operator++(STEPS& s)
{
s = staic_cast<STEPS>(static_cast<int>(s) + 1);
if (s == STEPS::END_OF_LIST)
{
s = static_cast<STEPS>(static_cast<int>(s) - 1);
}
return s;
}
在我的教程状态类的更新功能中,我检查鼠标是否单击。如果是我要增加步骤。
//这是在标题中定义的,并将其设置为初始化时的步骤::一个 STEPS steps;
TutorialState::Update()
{
// If mouse was clicked
if (mouse.Left())
{
steps++; // this is giving me an error.
}
}
STEPS& operator++(STEPS& s);
适用于++step
。
对于step++
,您需要
STEPS operator++(STEPS& s, int) { auto res = s; ++s; return res; }
已选择使用额外的参数 int
来区分预增量运算符。
您可以阅读http://en.cppreference.com/w/cpp/language/operator_incdec有关更多详细信息。
相关文章:
- 不带大括号的枚举形式
- 枚举环境变量的惯用C++14/C++17方法
- 类似枚举的计算常量
- 如何正确实现和访问运算符的各种自定义枚举器
- 错误:从"int"到枚举c++的转换无效
- C++中构造函数中的枚举
- 访问在 C++ 结构中声明的枚举变量
- 枚举类'classname'的多重定义
- 强枚举类型定义:Clang Bug 还是 C++11 标准不确定性?
- typedef 枚举和枚举类有什么区别?
- 为什么我的开关/机箱在使用枚举时默认?
- 标准::可选枚举的比较运算符
- C++两个源文件之间共享的枚举的静态实例
- 打印没有铸件的枚举可以在C++中吗?
- 枚举成员与静态 int 成员?
- C++:枚举:错误:应使用标识符而不是"}"
- 带有 c++ 的枚举(输入检查)
- 在 qml 中使用 Q_ENUM 和 Q_PROPERTY 作为枚举类
- C 操作员在枚举类中的过载
- 操作员超载枚举类