本帖最后由 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];
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 我是照着视屏中的代码打的 看了几遍没发现什么问题 这是为什么呢?求解答
|