Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Enumerations as loop variables (http://www.programmingforums.org/showthread.php?t=13881)

JawaKing00 Aug 31st, 2007 11:58 AM

Enumerations as loop variables
 
I've been programming in C mostly, and I've become accustomed to being able to do things like this:

:

typedef enum TEST_ENUM_t
{
    TEST0,
    TEST1,
    TEST2,
    TEST3
} TEST_ENUM_t;

TEST_ENUM_t TestIndex;

for (TestIndex = TEST0; TestIndex <= TEST3; TestIndex++)
{
    // Do something
}



Well, now I'm trying to do something like this for some test driver software written in Visual C++ which will be used to test C functions. When I attempt to do something like I have above in Visual C++, I get the following error:

:

error C2676: binary '++' : 'TEST_ENUM_t' does not define this operator or a conversion to a type acceptable to the predefined operator

Is there a way to do something similar to the code above in Visual C++?

Thank you.

DaWei Aug 31st, 2007 1:09 PM

In C an enum is an int. In C++ it is not. It is, in fact, its own datatype. One result of this is that the typedef is no longer required. Another result is that you can no longer perform normal arithmetic on it.

Is there any particular reason that you haven't merely defined TestIndex as an int?

l2u Sep 2nd, 2007 10:37 AM

I think it should work if you do something like this:

:

typedef enum TEST_ENUM_t {
    TEST0 = 0,
    TEST1,
    TEST2,
    TEST3
};


DaWei Sep 2nd, 2007 12:30 PM

Doesn't change a thing. TEST0 is, by default, zero.


All times are GMT -5. The time now is 2:59 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC