A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© sen 中级黑马   /  2014-5-20 11:21  /  1691 人查看  /  10 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 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 16:12

评分

参与人数 1技术分 +1 收起 理由
傘が咲く + 1

查看全部评分

10 个回复

倒序浏览
本帖最后由 欧翔 于 2014-5-20 13:51 编辑

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

   
}
      

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

     

评分

参与人数 1技术分 +1 收起 理由
傘が咲く + 1 代码请用代码插入格式

查看全部评分

回复 使用道具 举报
楼主好,我也是一个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
可以正常的输出,应该就是初始化类方法这里导致的问题,具体我还没有弄太明白,正在研究中,希望大家多多交流!

点评

继续加油,看好你!  发表于 2014-5-20 16:15
回复 使用道具 举报
楼主,不好意思,我又将你的代码敲了一遍,我这里输出是正常的!我再仔细看看你的代码!
回复 使用道具 举报
1楼说的对,就是你的类方法里Point2写成了Ponint2:如下!
+ (double)distanceBetweenPoint1:(Point2D *)p1 andPonint2:(Point2D *)p2
{
      return [p2 distanceWithOther:p1];
回复 使用道具 举报
学的这么快啊
回复 使用道具 举报
sen 中级黑马 2014-5-21 14:01:53
7#
欧翔 发表于 2014-5-20 13:37
你的调用语句写少了一个n。
      
+ (double)distanceBetweenPoint1Point2D *)p1 andPonint2Point2D * ...

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

非常感谢你的帮助,我也是OC的初学者,希望有机会我们多交流一下学习上得问题
回复 使用道具 举报
sen 发表于 2014-5-21 14:04
非常感谢你的帮助,我也是OC的初学者,希望有机会我们多交流一下学习上得问题 ...

好的,多多交流!!!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马