2011-01-13 79 views
0

我有兩個屬性,我宣佈我的頭文件,並在我的課合成(叫什麼名字?)文件:錯誤:沒有財產申報

  • 寬度
  • 高度

    (散/磅符號)導入

    @interface Rectangle : NSObject { int width; int height; } 
    
    @property width, height; 
    
    -(int) area; 
    -(int) perimeter; 
    -(void) setWidth: (int) w setHeight: (int) h; 
    
    @end 
    

.m文件:

(hash/pound symbol)import "Rectangle.h" 
@implementation Rectangle 

@synthesize width, height; 
-(void) setWidth: (int) w setHeight: (int) h 
{ 
    width = w; 
    height = h; 
} 
-(int) area 
{ 
    return width * height; 
} 
-(int) perimeter 
{ 
    return (width + height) * 2; 
} 
@end 

不過,我得到一些錯誤:

error: syntax error before 'width; 
error: no declaration of property 'width' found in the interface; 
error: no declaration of property 'height' found in the interface; 

請原諒我有以「#」符號和代碼格式問題的格式。

回答

3

我很驚訝......

@property width, height; 

...即使編譯。它應該是:

@property int width; 
@property int height; 

而且你幾乎從來沒有看到這一點:

-(void) setWidth: (int) w setHeight: (int) h; 

相反,@property意味着setWidth:setHeight:

+0

這工作,謝謝!我必須等待接受答案。 – redconservatory 2011-01-13 18:57:13

2

試試這個:

@interface Rectangle : NSObject 
{ 
    int width; 
    int height; 
} 

@property int width; 
@property int height; 

-(int) area; 
-(int) perimeter; 
-(void) setWidth: (int) w andHeight: (int) h; 

@end