Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

nashyo

macrumors 6502
Original poster
Oct 1, 2010
299
0
Bristol
I'm having trouble putting this stuff into some kind of struct.

Code:
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

Code:
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?
 
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.
 
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

Code:
#define West -M_PI/2

Expected Identifier or ')'.

Im assuming it's the way I've written it...
 
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:
Code:
#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.
 
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:
Code:
#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 ')'

Code:
#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;
 
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:
Code:
//#define West (-M_PI/2)

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

3. Remove the value:
Code:
#define West

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

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


Is your posted code in a .h file or a .m file?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.