2011-10-19 105 views
1

可能重複之前:
Expected 「;」 before 「{」 token?預期的「;」 {令牌

所以我得到的錯誤在一張從一本書上抄 接口定義後的代碼中定義了覆蓋方法。這是我似乎遇到問題的地方。試圖讓這個代碼工作,所以我可以繼續學習。

#import <Foundation/Foundation.h> 

@interface Sprite : NSObject{ 
    CGFloat x; 
    CGFloat y; 
    CGFloat r; 
    CGFloat g; 
    CGFloat b; 
    CGFloat alpha; //alpha value, for transparency 
    CGFloat speed; //speed of movement in pixels/frame 
    CGFloat angle; //angle of movement in degrees 
    CGFloat rotation; //rotation of our sprite in degrees about the center 
    CGFloat width; 
    CGFloat height; 
    CGFloat scale; //uniform scaling factor for size 
    int frame; //for animation 

    CGFloat cosTheta; //precomputed for speed 
    CGFloat sinTheta; 
    CGRect box; //our bounding box 

    BOOL render; //true when we're rendering 
    BOOL offScreen; //true when we're off the screen 
    BOOL wrap; //true if you want the motion to wrap the screen 
} 

@property (assign) BOOL wrap, render, offScreen; 
@property (assign) CGFloat x, y, r, g, b, alpha; 
@property (assign) CGFloat speed, angle, rotation; 
@property (assign) CGFloat width, height, scale; 
@property (assign) CGRect box; 
@property (assign) int frame; 

// THIS IS WHERE I'M GETTING THE ERROR 
- (void) setRotation: (CGFloat) degrees 
{ 
    rotation = degrees * 3.141592/180.0; 
} 

-(CGFloat) rotation 
{ 
    return rotation*180.0/3.141592; 
} 

- (void) setAngle: (CGFloat) degrees 
{ 
    angle = degrees*3.141592/180.0; 
    cosTheta = cos(angle); 
    sinTheta = sin(angle); 
} 

- (CGFloat) angle 
{ 
    return angle*180.0/3.141592; 
} 

@end 

回答

-3

只是一個猜測,但嘗試把一個分號像這樣:

BOOL render; //true when we're rendering 
BOOL offScreen; //true when we're off the screen 
BOOL wrap; //true if you want the motion to wrap the screen 
}; 
2

這是所有在同一個文件?您只應在.h文件中聲明方法簽名,並在實現部分中詳細說明。

所以,你的.h文件將是因爲它是,除了替代方法的詳細信息,您剛剛結束每個簽名用分號:

- (void) setRotation: (CGFloat) degrees; 
- (CGFloat) rotation; 
- (void) setAngle: (CGFloat) degrees; 
- (CGFloat) angle; 

並把實際執行中的.m文件。

1

一個接口只包含方法的原型,而不包含它們的內容。

你需要一個單獨的@implementation

1

您在.h文件中缺少@end,這就是您收到預期的原因;令牌之前

另外,如果您不在.h文件中聲明方法,則它們對於該類是私有的。

因此,創建.h文件中這一內容

#import <Foundation/Foundation.h> 

@interface Sprite : NSObject{ 
    CGFloat x; 
    CGFloat y; 
    CGFloat r; 
    CGFloat g; 
    CGFloat b; 
    CGFloat alpha; //alpha value, for transparency 
    CGFloat speed; //speed of movement in pixels/frame 
    CGFloat angle; //angle of movement in degrees 
    CGFloat rotation; //rotation of our sprite in degrees about the center 
    CGFloat width; 
    CGFloat height; 
    CGFloat scale; //uniform scaling factor for size 
    int frame; //for animation 

    CGFloat cosTheta; //precomputed for speed 
    CGFloat sinTheta; 
    CGRect box; //our bounding box 

    BOOL render; //true when we're rendering 
    BOOL offScreen; //true when we're off the screen 
    BOOL wrap; //true if you want the motion to wrap the screen 
} 

@property (assign) BOOL wrap, render, offScreen; 
@property (assign) CGFloat x, y, r, g, b, alpha; 
@property (assign) CGFloat speed, angle, rotation; 
@property (assign) CGFloat width, height, scale; 
@property (assign) CGRect box; 
@property (assign) int frame; 

- (void) setRotation: (CGFloat) degrees; 
- (CGFloat) rotation; 
- (void) setAngle: (CGFloat) degrees; 
- (CGFloat) angle; 

@end 

然後在.m文件本

#import "Sprite.h" 

@implementation Sprite 

- (void) setRotation: (CGFloat) degrees 
{ 
    rotation = degrees * 3.141592/180.0; 
} 

-(CGFloat) rotation 
{ 
    return rotation*180.0/3.141592; 
} 
- (void) setAngle: (CGFloat) degrees 
{ 
    angle = degrees*3.141592/180.0; 
    cosTheta = cos(angle); 
    sinTheta = sin(angle); 
} 

- (CGFloat) angle 
{ 
    return angle*180.0/3.141592; 
} 

@end