Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 31st, 2007, 10:58 AM   #1
JawaKing00
Newbie
 
Join Date: Sep 2004
Posts: 29
Rep Power: 0 JawaKing00 is on a distinguished road
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.
JawaKing00 is offline   Reply With Quote
Old Aug 31st, 2007, 12:09 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Sep 2nd, 2007, 9:37 AM   #3
l2u
Newbie
 
Join Date: Mar 2007
Posts: 24
Rep Power: 0 l2u is on a distinguished road
I think it should work if you do something like this:

typedef enum TEST_ENUM_t {
    TEST0 = 0,
    TEST1,
    TEST2,
    TEST3
};
l2u is offline   Reply With Quote
Old Sep 2nd, 2007, 11:30 AM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Doesn't change a thing. TEST0 is, by default, zero.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
for loop variables emdiesse Java 8 May 17th, 2007 10:20 PM
Hanging For loop in C++ can342man C++ 20 Apr 30th, 2006 7:27 AM
Help in QBASIC (I think it's similar to VB) phoenix987 Visual Basic 3 May 9th, 2005 12:33 PM
Help with a QBASIC program phoenix987 Other Programming Languages 4 May 5th, 2005 12:27 PM
Timing loop problems badbasser98 C++ 11 Mar 10th, 2005 8:30 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:19 AM.

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