本帖最后由 chain 于 2014-6-7 21:39 编辑
为什么编译器总是报这个错呢? 求解答 ,谢谢
10.m:83:13: error: unknown receiver 'Point1'; did you mean 'Point2D'? return [Point1 distanceWithOther:point2]; ^~~~~~ Point2D 10.m:10:12: note: 'Point2D' declared here @interface Point2D : NSObject ^ 10.m:83:20: warning: class method '+distanceWithOther:' not found (return type defaults to 'id') [-Wobjc-method-access] return [Point1 distanceWithOther:point2]; ^~~~~~~~~~~~~~~~~ 10.m:83:12: error: returning 'id' from a function with incompatible result type 'double' return [Point1 distanceWithOther:point2]; ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 warning and 2 errors generated. - #import <Foundation/Foundation.h>
- #import <math.h>
- @interface Point2D : NSObject
- {
- double _x;
- double _y;
- }
- // x的setter和getter
- - (void)setX:(double)x;
- - (double)x;
- // y的setter和getter
- - (void)setY:(double)y;
- - (double)y;
- //同时设置x和y
- - (void)setX:(double)x andY:(double)y;
- //计算和其他点的距离
- - (double)distanceWithOther:(Point2D *)other;
- //类方法计算两个点之间的距离
- + (double)distanceBetweenPoint1:(Point2D *)point1 andPoint2:(Point2D *)point2;
- @end
- @implementation Point2D
- // x的setter和getter
- - (void)setX:(double)x
- {
- _x = x;
- }
- - (double)x
- {
- return _x;
- }
- // y的setter和getter
- - (void)setY:(double)y
- {
- _y = y;
- }
- - (double)y
- {
- return _y;
- }
- //同时设置x和y
- - (void)setX:(double)x andY:(double)y
- {
- _x = x;
- _y = y;
- }
- //计算和其他点的距离
- - (double)distanceWithOther:(Point2D *)other
- {
- //计算X的差值
- double xMinus = [self x] - [other x];
- //计算Y的差值
- double yMinus = [self y] - [other y];
- //计算X差值的平方
- double xSquare = pow(xMinus,2);
- //计算Y差值的平方
- double ySquare = pow(yMinus,2);
- //计算xMinus和yMinus的和的开跟
- return sqrt(xSquare + ySquare);
- }
- //类方法计算两个点之间的距离
- + (double)distanceBetweenPoint1:(Point2D *)point1 andPoint2:(Point2D *)point2
- {
- //代码重构
- return [Point1 distanceWithOther:point2];
- }
- @end
- int main ()
- {
- Point2D *point1 = [Point2D new];
- Point2D *point2 = [Point2D new];
- [point1 setX:20 andY:40];
- [point2 setX:18 andY:28];
- double c = [point1 distanceWithOther:point2];
- double c1 = [Point2D distanceBetweenPoint1:point1 andPoint2:point2];
- NSLog(@"%f----%f",c,c1);
- return 0;
- }
复制代码
|