如何在 Visual Studio C++ 中增加枚举6.0?

发布于 2024-09-07 07:24:02 字数 363 浏览 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

偏爱你一生 2024-09-14 07:24:02

我认为在一个易于理解的枚举上定义operator++没有什么问题。这不就是运算符重载的目的吗?如果上下文没有意义(例如,其中有漏洞的枚举),那么它当然没有意义。为实现复数的名为 Complex 的类定义 operator* 不仅有效,而且是 C++ 中数学运算符重载的一个很好的应用!

如果开发人员定义了一个枚举,其中运算符++对该枚举的客户端具有明显且直观的意义,那么这是该运算符重载的一个很好的应用。

enum DayOfWeek {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};
inline DayOfWeek operator++(DayOfWeek &eDOW, int)
{
   const DayOfWeek ePrev = eDOW;
   const int i = static_cast<int>(eDOW);
   eDOW = static_cast<DayOfWeek>((i + 1) % 7);
   return ePrev;
}

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.

enum DayOfWeek {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};
inline DayOfWeek operator++(DayOfWeek &eDOW, int)
{
   const DayOfWeek ePrev = eDOW;
   const int i = static_cast<int>(eDOW);
   eDOW = static_cast<DayOfWeek>((i + 1) % 7);
   return ePrev;
}
喜爱纠缠 2024-09-14 07:24:02

枚举可能是整数,但这并不意味着它涵盖连续范围。

enum {
  A,
  B,
  C,
}

将默认为

enum {
  A = 0,
  B = A + 1,
  C = B + 1,
}

,因此您可以逃脱

int a = A;
a++;

但是,如果您

enum {
  A = 2,
  B = 4,
  C = 8,
}

现在有 +1 将不起作用。

现在,如果您

enum {
  FIRST,
  A = FIRST,
  B,
  C,
  LAST = C
}

在迭代枚举时也遇到类似的情况,您会执行 A 和 C 两次吗?

迭代枚举的目的是什么?您希望“针对所有人”还是针对某些子集?枚举实际上有顺序吗?

我将它们全部扔进一个容器中并迭代它而不是

  • 无序的 - 使用一组
  • 有序的 - 向量或列表

An enum may be integral, but it doesn't mean it covers a continuous range.

This

enum {
  A,
  B,
  C,
}

It will default to

enum {
  A = 0,
  B = A + 1,
  C = B + 1,
}

and so you could get away with

int a = A;
a++;

However if you have

enum {
  A = 2,
  B = 4,
  C = 8,
}

now +1 ain't gonna work.

Now, if you also had things like

enum {
  FIRST,
  A = FIRST,
  B,
  C,
  LAST = C
}

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

  • unordered - use a set
  • ordered - a vector or list
萌酱 2024-09-14 07:24:02

请尝试转换为 int,加一 (+1),然后转换回 enum。

Please try to convert to int, add one (+1) and convert back to the enum.

十年九夏 2024-09-14 07:24:02
myenum=(myenum_type)((int)myenum+1);

它很丑,但很有效。

myenum=(myenum_type)((int)myenum+1);

It's ugly but it works.

猫弦 2024-09-14 07:24:02
enum xyz
{
a,
b,
c};

xyz val = a;

//try this

val = xyz(val +1);
enum xyz
{
a,
b,
c};

xyz val = a;

//try this

val = xyz(val +1);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文