如何在 Visual Studio C++ 中增加枚举6.0?
我复制并粘贴了一些递增枚举的代码:
myenum++;
此代码工作正常,因为它是在 VS 中编译的.NET C++ 2003。
我现在在 Visual Studio 6.0 中进行开发并收到错误:
错误 C2676:二进制“++”:“枚举” ID' 没有定义这个 运算符或类型转换 预定义运算符可接受
我怎样才能让它在 6.0 中表现相同?
I copy and pasted some code that increments an enum:
myenum++;
This code worked fine as it was compiled in VS.NET C++ 2003.
I am now developing in Visual Studio 6.0 and get the error:
error C2676: binary '++' : 'enum
ID' does not define this
operator or a conversion to a type
acceptable to the predefined operator
How can I get this to behave the same in 6.0?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为在一个易于理解的枚举上定义operator++没有什么问题。这不就是运算符重载的目的吗?如果上下文没有意义(例如,其中有漏洞的枚举),那么它当然没有意义。为实现复数的名为 Complex 的类定义 operator* 不仅有效,而且是 C++ 中数学运算符重载的一个很好的应用!
如果开发人员定义了一个枚举,其中运算符++对该枚举的客户端具有明显且直观的意义,那么这是该运算符重载的一个很好的应用。
I see nothing wrong with defining operator++ on a well understood enum. Isn't that the purpose of operator overloading? If the context made no sense (e.g. an enum with holes in it), then of course it doesn't make sense. Defining operator* for a class called Complex that implement complex numbers is not just valid but a great application of mathematical operator overloading in C++!
If the developer defines an enum where operator++ makes obvious and intuitive sense to the clients of that enum, then that's a good application of that operator overload.
枚举可能是整数,但这并不意味着它涵盖连续范围。
这
将默认为
,因此您可以逃脱
但是,如果您
现在有 +1 将不起作用。
现在,如果您
在迭代枚举时也遇到类似的情况,您会执行 A 和 C 两次吗?
迭代枚举的目的是什么?您希望“针对所有人”还是针对某些子集?枚举实际上有顺序吗?
我将它们全部扔进一个容器中并迭代它而不是
An enum may be integral, but it doesn't mean it covers a continuous range.
This
It will default to
and so you could get away with
However if you have
now +1 ain't gonna work.
Now, if you also had things like
then when iterating the enum would you do A and C twice?
What is the purpose of iterating the enum? Do you wish to do 'for all' or for some subset? Is there actually an order to the enum?
I'd throw them all in a container and iterate that instead
请尝试转换为 int,加一 (+1),然后转换回 enum。
Please try to convert to int, add one (+1) and convert back to the enum.
它很丑,但很有效。
It's ugly but it works.