From: Larry Lindstrom on
Hello Folks:

Developing on Win XP, VS 2008 Pro, C++.

I can't believe my eyes.

I've auto-incremented enumerators many times.

I've moved a 50 line function from one library to another library
and now the auto increment of an enumerator gets an error

I simplified the code into the following console app.

This code gets the same error on the ++test statement.

1>c:\documents and settings\...\enum_problem.cpp(11) : error C2675:
unary '++' : 'main::TEST_ENUM' does not define this operator or a
conversion to a type acceptable to the predefined operator

int main(int argc, char **argv)
{
enum TEST_ENUM
{
TEST_0,
TEST_1
};


TEST_ENUM test = TEST_0;
++test;
}

What am I doing wrong?

It's 2:00 AM here on the US West coast. Time for bed.

Thanks
Larry
From: Ian Collins on
On 06/28/10 09:00 PM, Larry Lindstrom wrote:
> Hello Folks:
>
> Developing on Win XP, VS 2008 Pro, C++.
>
> I can't believe my eyes.
>
> I've auto-incremented enumerators many times.

Not with a conforming compiler you haven't!

> int main(int argc, char **argv)
> {
> enum TEST_ENUM
> {
> TEST_0,
> TEST_1
> };
>
>
> TEST_ENUM test = TEST_0;
> ++test;
> }
>
> What am I doing wrong?

operator ++ isn't defined for enum types.

--
Ian Collins
From: Ike Naar on
In article <i09od2$pat$1(a)news.eternal-september.org>,
Larry Lindstrom <larryl_turbo(a)hotmail.com> wrote:
>{
> enum TEST_ENUM
> {
> TEST_0,
> TEST_1
> };
>
> TEST_ENUM test = TEST_0;
> ++test;
>}

In C++ you cannot apply ++ to a variable that has an enumeration type.
Rewrite it as:

test = static_cast<TEST_ENUM>(test+1);

You say you have used the ++ operator like that many times before.
What that in C code, or in C++ code? (in C it's allowed).
From: Stuart Golodetz on
On 28/06/2010 10:07, Ian Collins wrote:
> On 06/28/10 09:00 PM, Larry Lindstrom wrote:
>> Hello Folks:
>>
>> Developing on Win XP, VS 2008 Pro, C++.
>>
>> I can't believe my eyes.
>>
>> I've auto-incremented enumerators many times.
>
> Not with a conforming compiler you haven't!
>
>> int main(int argc, char **argv)
>> {
>> enum TEST_ENUM
>> {
>> TEST_0,
>> TEST_1
>> };
>>
>>
>> TEST_ENUM test = TEST_0;
>> ++test;
>> }
>>
>> What am I doing wrong?
>
> operator ++ isn't defined for enum types.

But you can write your own (this works on both MSVC and g++ - I'm
assuming it's standard-conforming unless there's some obscure rule I've
missed?) For example:

TEST_ENUM& operator++(TEST_ENUM& e)
{
e = TEST_ENUM(e + 1);
return e;
}

Or (if you prefer to make the cast going on more obvious):

TEST_ENUM& operator++(TEST_ENUM& e)
{
e = static_cast<TEST_ENUM>(e + 1);
return e;
}

If you explicitly set values in your enum, you might need to do
something more complicated than this in operator++, of course.

Cheers,
Stu
From: Larry Lindstrom on
On 6/28/2010 3:29 AM, Stuart Golodetz wrote:
> On 28/06/2010 10:07, Ian Collins wrote:
>> On 06/28/10 09:00 PM, Larry Lindstrom wrote:
>>> Hello Folks:
>>>
>>> Developing on Win XP, VS 2008 Pro, C++.
>>>
>>> I can't believe my eyes.
>>>
>>> I've auto-incremented enumerators many times.
>>
>> Not with a conforming compiler you haven't!
>>
>>> int main(int argc, char **argv)
>>> {
>>> enum TEST_ENUM
>>> {
>>> TEST_0,
>>> TEST_1
>>> };
>>>
>>>
>>> TEST_ENUM test = TEST_0;
>>> ++test;
>>> }
>>>
>>> What am I doing wrong?
>>
>> operator ++ isn't defined for enum types.
>
> But you can write your own (this works on both MSVC and g++ - I'm
> assuming it's standard-conforming unless there's some obscure rule I've
> missed?) For example:
>
> TEST_ENUM& operator++(TEST_ENUM& e)
> {
> e = TEST_ENUM(e + 1);
> return e;
> }
>
> Or (if you prefer to make the cast going on more obvious):
>
> TEST_ENUM& operator++(TEST_ENUM& e)
> {
> e = static_cast<TEST_ENUM>(e + 1);
> return e;
> }
>
> If you explicitly set values in your enum, you might need to do
> something more complicated than this in operator++, of course.

Thaks Everybody:

Yea, I was looking at an enumerator I wrote several years ago. Upon
further examination, I found that I had added these operators.

That qualifies as a stupid mistake doesn't it?

Larry