Obj-C“冲突类型/先前声明”错误

发布于 11-17 17:59 字数 2785 浏览 4 评论 0原文

Obj-C 初学者,我在实现文件的以下行中收到“冲突类型.../先前声明...”错误:

-(void) setWidth: (double) w andHeight: (double) h
-(double) area
-(double) perimeter

这是 .h 和 .m:

#import "GraphicObject.h"
#import "XYPOINT.h"  

@interface Rectangle : GraphicObject 

{

    double width;
    double height;
    XYPOINT *origin;

}

@property double width, height;

-(XYPOINT *) origin;
-(void) setOrigin: (XYPOINT *) pt;
-(void) setWidth: (double) w andHeight: (double) h;
-(double) area;
-(double) perimeter;
-(void) translate: (XYPOINT *) v;
-(Rectangle *) intersect: (Rectangle *) r2;
-(void) draw;
-(id) initWithWidth: (double) w andHeight: (double) h;

@end

#import "Rectangle.h"


@implementation Rectangle

@synthesize width, height;

-(void) setWidth: (double) w andHeight: (double) h
{
    width = w;
    height = h;
}

-(double) area
{
    return width * height;
}

-(double) perimeter
{
    return (width + height) * 2;
}

-(void) setOrigin: (XYPOINT *) pt
{
    origin = [[XYPOINT alloc] init];

    [origin setX: pt.x andY: pt.y];
}


-(XYPOINT *) origin
{
    return origin;
}

-(void) translate: (XYPOINT *) v
{
    origin.x += v.x;
    origin.y += v.y;

}

-(Rectangle *) intersect: (Rectangle *) r2

{

    Rectangle *result = [[Rectangle alloc] init];
    XYPOINT *oPoint = [[XYPOINT alloc] init];
    double resultW, resultH, resultX, resultY, deuceX, deuceY;


    resultX = (origin.x > r2.origin.x) ? origin.x : r2.origin.x;
    resultY = (origin.y > r2.origin.y) ? origin.y : r2.origin.y;
    [oPoint setX: resultX andY: resultY];

    deuceX = (origin.x + width < r2.origin.x + r2.width) ? 
    origin.x + width : r2.origin.x + r2.width;

    deuceY = (origin.y + height < r2.origin.y + r2.height) ?
    origin.y + height : r2.origin.y + r2.height;


    resultW = deuceX - resultX;

    resultH = deuceY - resultY;


    if ((resultY >= deuceY) || (resultX >= deuceX)){

        resultW = 0;
        resultH = 0;
        [oPoint setX: 0 andY: 0];
    }




    [result setWidth: resultW andHeight: resultH];
    [result setOrigin: oPoint];

    return result;


}

-(void) draw
{
    for (int h = 0 ; h < height + 2 ; ++h){
        for (int w = 0 ; w < width ; ++w){
            if (h == 0 || h == height + 1){
                printf("-");
            } else {
                if (w == 0 || w == width - 1){
                    printf ("|");
                } else {
                    printf(" ");
                }
            }
        }
        printf("\n");
    }
}

-(Rectangle *) initWithWidth: (double) w andHeight: (double) h

{

    self = [super init];

    if (self)
        [self setWidth: w andHeight: h];

    return self;

}




@end

希望这将是足够的代码来理解问题。

Obj-C beginner, I'm getting "Conflicting types for.../ Previous declaration of..." errors on the following lines of the implementation file:

-(void) setWidth: (double) w andHeight: (double) h
-(double) area
-(double) perimeter

Here's the .h and .m:

#import "GraphicObject.h"
#import "XYPOINT.h"  

@interface Rectangle : GraphicObject 

{

    double width;
    double height;
    XYPOINT *origin;

}

@property double width, height;

-(XYPOINT *) origin;
-(void) setOrigin: (XYPOINT *) pt;
-(void) setWidth: (double) w andHeight: (double) h;
-(double) area;
-(double) perimeter;
-(void) translate: (XYPOINT *) v;
-(Rectangle *) intersect: (Rectangle *) r2;
-(void) draw;
-(id) initWithWidth: (double) w andHeight: (double) h;

@end

#import "Rectangle.h"


@implementation Rectangle

@synthesize width, height;

-(void) setWidth: (double) w andHeight: (double) h
{
    width = w;
    height = h;
}

-(double) area
{
    return width * height;
}

-(double) perimeter
{
    return (width + height) * 2;
}

-(void) setOrigin: (XYPOINT *) pt
{
    origin = [[XYPOINT alloc] init];

    [origin setX: pt.x andY: pt.y];
}


-(XYPOINT *) origin
{
    return origin;
}

-(void) translate: (XYPOINT *) v
{
    origin.x += v.x;
    origin.y += v.y;

}

-(Rectangle *) intersect: (Rectangle *) r2

{

    Rectangle *result = [[Rectangle alloc] init];
    XYPOINT *oPoint = [[XYPOINT alloc] init];
    double resultW, resultH, resultX, resultY, deuceX, deuceY;


    resultX = (origin.x > r2.origin.x) ? origin.x : r2.origin.x;
    resultY = (origin.y > r2.origin.y) ? origin.y : r2.origin.y;
    [oPoint setX: resultX andY: resultY];

    deuceX = (origin.x + width < r2.origin.x + r2.width) ? 
    origin.x + width : r2.origin.x + r2.width;

    deuceY = (origin.y + height < r2.origin.y + r2.height) ?
    origin.y + height : r2.origin.y + r2.height;


    resultW = deuceX - resultX;

    resultH = deuceY - resultY;


    if ((resultY >= deuceY) || (resultX >= deuceX)){

        resultW = 0;
        resultH = 0;
        [oPoint setX: 0 andY: 0];
    }




    [result setWidth: resultW andHeight: resultH];
    [result setOrigin: oPoint];

    return result;


}

-(void) draw
{
    for (int h = 0 ; h < height + 2 ; ++h){
        for (int w = 0 ; w < width ; ++w){
            if (h == 0 || h == height + 1){
                printf("-");
            } else {
                if (w == 0 || w == width - 1){
                    printf ("|");
                } else {
                    printf(" ");
                }
            }
        }
        printf("\n");
    }
}

-(Rectangle *) initWithWidth: (double) w andHeight: (double) h

{

    self = [super init];

    if (self)
        [self setWidth: w andHeight: h];

    return self;

}




@end

Hopefully that will be enough code to understand the problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文