Structs and Enum problems

Discussion in 'iPhone/iPad Programming' started by nashyo, Sep 9, 2012.

  1. macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Messages:
    299
    Location:
    Bristol
    #1
    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?
     
  2. macrumors 603

    Joined:
    Aug 9, 2009
    Messages:
    6,404
    #2
    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.
     
  3. macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Messages:
    299
    Location:
    Bristol
    #3
    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...
     
  4. macrumors 603

    Joined:
    Aug 9, 2009
    Messages:
    6,404
    #4
    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.
     
  5. macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Messages:
    299
    Location:
    Bristol
    #5
    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;
     
  6. macrumors 603

    Joined:
    Aug 9, 2009
    Messages:
    6,404
    #6
    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?
     

Share This Page