错误:没有声明属性

时间:2011-01-13 18:46:53

标签: objective-c

我在头文件中声明了两个属性,并在我的类(名称?)文件中合成:

  • 宽度
  • 高度

    (哈希/井号)导入

    @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;

请原谅格式化我遇到'#'符号和代码格式问题。

2 个答案:

答案 0 :(得分:3)

我很惊讶......

@property width, height;

......甚至编译。它应该是:

@property int width;
@property int height;

你几乎从未见过这个:

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

相反,@ property表示setWidth:setHeight:

答案 1 :(得分: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