PDA

View Full Version : Structs and Enum problems




nashyo
Sep 9, 2012, 10:35 AM
I'm having trouble putting this stuff into some kind of struct.

const CGFloat North = 0;
const CGFloat East = M_PI/2;
const CGFloat West = -M_PI/2;
const CGFloat SouthPositive = M_PI;
const CGFloat SouthNegative = -M_PI;

const CGFloat NNE = M_PI/4;
const CGFloat NNW = -M_PI/4;
const CGFloat SSE = M_PI - (M_PI/4);
const CGFloat SSW = -M_PI + (M_PI/4);

like so

typedef enum {
North = 0,
East = M_PI/2,
West = -M_PI/2,
SouthPositive = M_PI,
SouthNegative = -M_PI,

NNE = M_PI/4,
NNW = -M_PI/4,
SSE = M_PI - (M_PI/4),
SSW = -M_PI + (M_PI/4),
} Foo;

West, SouthNegative, NNW and SSW are giving errors 'expression is not an integer constant expression'.

What's the deal?



chown33
Sep 9, 2012, 10:52 AM
Enums are sets of integer values. No other types (e.g. floating-point) are allowed. Look up the enum keyword in a C reference.

You could use #defines instead of an enum.

nashyo
Sep 9, 2012, 10:58 AM
Enums are sets of integer values. No other types (e.g. floating-point) are allowed. Look up the enum keyword in a C reference.

You could use #defines instead of an enum.

I'm getting a different error message with this

#define West -M_PI/2

Expected Identifier or ')'.

Im assuming it's the way I've written it...

chown33
Sep 9, 2012, 12:25 PM
Post the exact code that produces the error. If there's code before the #define you've posted, post that. Errors don't occur in isolation. We need to see the context.

Also, as a general rule, write #defines like this:
#define NAME (expression)
If it's not clear why parentheses around the expression might be helpful, study a C reference on exactly how #defines work, and then think about how the expansion of a textual macro might be parsed depending on the context in which it's used.

nashyo
Sep 9, 2012, 12:28 PM
Post the exact code that produces the error. If there's code before the #define you've posted, post that. Errors don't occur in isolation. We need to see the context.

Also, as a general rule, write #defines like this:
#define NAME (expression)
If it's not clear why parentheses around the expression might be helpful, study a C reference on exactly how #defines work, and then think about how the expansion of a textual macro might be parsed depending on the context in which it's used.

This is the code before and after. Two errors on line starting #define.
1) Expected '('
2)Expected identifier or ')'

#import "VAModalImageController.h"
#import <QuartzCore/QuartzCore.h>

#define West (-M_PI/2)

@interface VAModalImageController () <UIScrollViewDelegate>
@end

@implementation VAModalImageController {
float fromValue;
float toValue;
}

@synthesize image = _image;
@synthesize scrollView = _scrollView;
@synthesize imageView = _imageView;

chown33
Sep 9, 2012, 01:19 PM
I see no obvious reason for the posted #define to cause an error.

Make test cases. Compile them. Post the results.

1. Comment out the #define:
//#define West (-M_PI/2)


2. Remove all code after the #define:
#define West (-M_PI/2)
.. file ends here ..


3. Remove the value:
#define West


4. Change the name:
#define MY_WEST (-M_PI/2)


5. Change the expression:
#define West (-3.14/2)



Is your posted code in a .h file or a .m file?