开发者

Error: no declaration of property

I have two properties I declare in my header file and synthesize in my class (name?) file:

  • width
  • height

    (hash/pound symbol)import

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

.m file:

 (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

However, I am getting some errors:

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;

Please excuse the formatting I am having problems with the '#' symbol and the code formatting.


I'm surprised...

@property width, height;

... that even compiles. It should be:

@property int width;
@property int height;

And you'd almost never see this:

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

Instead, the @property implies setWidth: and setHeight:.


Try this:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜