PDA

View Full Version : Enum problems




blue-lion
Jan 5, 2011, 02:29 AM
Wirelessly posted (Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_2_1 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8C148a Safari/6533.18.5)

Hello again, i am experiencing a few problems using 'typedef enum' statement. i would like to define variable types which will be used through out the program , so i thought i should put the definition in the header file. It all compiles ok and appeared to be going well,but when i step through into my implementation file i notice the variables lose their values?. Should the typedef be repeated in the implementation file as well?. Below is my header file and main file.


#import <Foundation/Foundation.h>

typedef enum cardFace { ace=1,two=2,three=3,four=4,five=5,six=6,
seven=7,eight=8,nine=9,ten=10,jack=10,
queen=10,king=10} suite;

typedef enum suite {spades=1,diamonds=2,clubs=3,hearts=4} cardFace;


@interface Card : NSObject {

suite varSuite;
cardFace varFace;

}
-(void) setCard: (cardFace) vF of : (suite) vS;

@end




#import <Foundation/Foundation.h>
#import "Card.h"

//enum flag {cross,circle};


int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

Card* Card1 =[[ Card alloc] init ] ;

[Card1 setCard:three of:diamonds];

[pool drain];
return 0;
}



chown33
Jan 5, 2011, 10:28 AM
[SIZE=1]

#import <Foundation/Foundation.h>

typedef enum cardFace { ace=1,two=2,three=3,four=4,five=5,six=6,
seven=7,eight=8,nine=9,ten=10,jack=10,
queen=10,king=10} suite;

typedef enum suite {spades=1,diamonds=2,clubs=3,hearts=4} cardFace;


@interface Card : NSObject {

suite varSuite;
cardFace varFace;

}
-(void) setCard: (cardFace) vF of : (suite) vS;

@end



I don't know what you're expecting to see in the debugger, but your enum names (blue) don't match your typedef names (red). Since your class is using the typedef, it's almost certainly producing the opposite of what you may be expecting. That's just a guess, since you didn't describe what you expect nor what you actually saw.

If the way to fix it isn't immediately obvious, then you need to go back and review the fundamentals of what a typedef is and how it works.

You also haven't posted any code for Card.m, so no one can compile it and attempt to duplicate the problem.


In the future, you always need to describe two things:
1. What you expected to happen.
2. What actually happened.

I don't know what "losing their values" means, and I don't think anyone else does. If you had described it as "I expected to see a suit of 1, but I saw 3", then it's clear what value you actually saw and what you expected. "Losing their values" could mean almost anything.

lloyddean
Jan 5, 2011, 11:09 AM
Just a note - the following is equivalent to your posted code. Note 'enum', or enumerate, will automatically increment to the next entry value so there, in this case at least, there is no reason to explicitly assign values until the first face card is reached.


typedef enum cardFace
{
ace = 1
, two // will be 2
, three // will be 3
, four // will be 4
, five // will be 5
, six // will be 6
, seven // will be 7
, eight // will be 8
, nine // will be 9
, ten // will be 10
, jack = 10 // would've been 11 but for explicit assignment of 10
, queen = 10 // would've been 11 but for explicit assignment of 10
, king = 10 // would've been 11 but for explicit assignment of 10
} suite;