2011-01-13 93 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