黑马程序员技术交流社区

标题: 为什么调用不了类方法? [打印本页]

作者: sen    时间: 2014-5-20 11:21
标题: 为什么调用不了类方法?
本帖最后由 sen 于 2014-5-21 14:04 编辑

/*
这是求两点距离的问题
*/

#import <Foundation/Foundation.h>
#import <math.h>

// 点
@interface Point2D : NSObject
{
    double _x; // x值
    double _y; // y值
}

// x值的getter和setter
- (void)setX:(double)x;
- (double)x;

//y值的getter和setter
- (void)setY:(double)y;
- (double)y;

//同时设置x和y
- (void)setX:(double)x andY:(double)y;

// 计算跟其他点的距离
- (double)distanceWithOther:(Point2D *)other;
//计算两个点之间的距离
+ (double)distanceBetweenPoint1:(Point2D *)p1 andPonint2:(Point2D *)p2;
@end

@implementation Point2D
// x值的getter和setter
- (void)setX:(double)x
{
    _x = x;
}
- (double)x
{
    return _x;
}

//y值的getter和setter
- (void)setY:(double)y
{
    _y = y;
}
- (double)y
{
    return _y;
}

//同时设置x和y
- (void)setX:(double)x andY:(double)y
{
    /*
    _x = x;
    _y = y;
     */
    /*
    self->_x = x;
    self->y = y;  */

    [self setX:x];
    [self setY:y];
}

// 计算跟其他点的距离
- (double)distanceWithOther:(Point2D *)other
{
    // ( (x1-x2)的平方 + (y1-y2)的平方 )开跟
    //return [Point2D distanceBetweenPoint1:self andPoint2:other];


    //x1-x2
    double xDelta = [self x] - [other x];
    //(x1-x2)的平方
    double xDeltaPF = pow(xDelta,2);

    //y1-y2
    double yDelta = [self y] - [other y];
    //(y1-y2)的平方
    double yDeltaPF = pow(yDelta,2);

    //返回距离
    return sqrt(xDeltaPF + yDeltaPF);   
}
//计算两个点之间的距离
+ (double)distanceBetweenPoint1:(Point2D *)p1 andPonint2:(Point2D *)p2
{
      return [p2 distanceWithOther:p1];

   
}
@end

int main()
{
    Point2D *p1 = [Point2D new];
    //(10,15)
    [p1 setX:10 andY:15];

    Point2D *p2 = [Point2D new];
    //(13,19)
    [p2 setX:13 andY:10];

    //double d1 = [p1 distanceWithOther:p2];
    double d1 = [Point2D distanceBetweenPoint1:p1 andPoint2:p2];
    NSLog(@"%f",d1);
    return 0;
}


编译器报的错是:
02-点得作业.m:138:26: warning: class method
      '+distanceBetweenPoint1:andPoint2:' not found (return type defaults to
      'id') [-Wobjc-method-access]
    double d1 = [Point2D distanceBetweenPoint1:p1 andPoint2:p2];
                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
02-点得作业.m:138:12: error: initializing 'double' with an expression of
      incompatible type 'id'
    double d1 = [Point2D distanceBetweenPoint1:p1 andPoint2:p2];

           ^    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~   
我是照着视屏中的代码打的 看了几遍没发现什么问题 这是为什么呢?求解答


作者: 欧翔    时间: 2014-5-20 13:37
本帖最后由 欧翔 于 2014-5-20 13:51 编辑

你的调用语句写少了一个n。
      
+ (double)distanceBetweenPoint1:(Point2D *)p1 andPonint2:(Point2D *)p2
{
      return [p2 distanceWithOther:p1];

   
}
      

double d1 = [Point2D distanceBetweenPoint1:p1 andPoint2:p2];

     

作者: huangqiwa    时间: 2014-5-20 15:00
楼主好,我也是一个oc初学者,我将你的代码自己敲了一遍,发现
double d1 = [Point2D distanceBetweenPoint1:p1 andPoint2:p2] 这句代码编译器提示 d1未使用
然后我查了下 类方法在初始化的时候 会返回id类型的数据,因此你编译的时候他会报错
initializing 'double' with an expression of
      incompatible type 'id'
我将你的代码修改为
NSLog(@"两点之间的距离为:%f",[Point2D distanceBetweenPoint1:p1 andPoint2:p2]);直接用NSLog输出没有复制给double的 d1
可以正常的输出,应该就是初始化类方法这里导致的问题,具体我还没有弄太明白,正在研究中,希望大家多多交流!
作者: huangqiwa    时间: 2014-5-20 15:20
楼主,不好意思,我又将你的代码敲了一遍,我这里输出是正常的!我再仔细看看你的代码!
作者: huangqiwa    时间: 2014-5-20 15:22
1楼说的对,就是你的类方法里Point2写成了Ponint2:如下!
+ (double)distanceBetweenPoint1:(Point2D *)p1 andPonint2:(Point2D *)p2
{
      return [p2 distanceWithOther:p1];
作者: Sniper_YJ    时间: 2014-5-20 15:27
学的这么快啊
作者: sen    时间: 2014-5-21 14:01
欧翔 发表于 2014-5-20 13:37
你的调用语句写少了一个n。
      
+ (double)distanceBetweenPoint1Point2D *)p1 andPonint2Point2D * ...

一楼正解 太厉害了!!我看了几遍都还是没找到 非常感谢~~
作者: sen    时间: 2014-5-21 14:04
huangqiwa 发表于 2014-5-20 15:00
楼主好,我也是一个oc初学者,我将你的代码自己敲了一遍,发现
double d1 =  这句代码编译器提示 d1未使用
...

非常感谢你的帮助,我也是OC的初学者,希望有机会我们多交流一下学习上得问题
作者: huangqiwa    时间: 2014-5-21 16:04
sen 发表于 2014-5-21 14:04
非常感谢你的帮助,我也是OC的初学者,希望有机会我们多交流一下学习上得问题 ...

好的,多多交流!!!




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2