加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

objective-c – 不完整的实现(xcode错误?)

发布时间:2020-12-14 19:26:53 所属栏目:百科 来源:网络整理
导读:// 9.1.h #import Foundation/Foundation.h@interface Complex : NSObject { double real; double imaginary;}@property double real,imaginary;-(void) print;-(void) setReal: (double) andImaginary: (double) b;-(Complex *) add: (Complex *) f;@end #i
// 9.1.h

#import <Foundation/Foundation.h>


@interface Complex : NSObject 
{

    double real;
    double imaginary;

}

@property double real,imaginary;
-(void) print;
-(void) setReal: (double) andImaginary: (double) b;
-(Complex *) add: (Complex *) f;

@end
#import "9.1.h"


@implementation Complex

@synthesize real,imaginary;

-(void) print
{
    NSLog(@ "%g + %gi ",real,imaginary);
}

-(void) setReal: (double) a andImaginary: (double) b
{
    real = a;
    imaginary = b;
}

-(Complex *) add: (Complex *) f
{
    Complex *result = [[Complex alloc] init];

    [result setReal: real + [f real] andImaginary: imaginary + [f imaginary]];

    return result;

}
@end

在最后的@end行,Xcode告诉我实现不完整.代码仍然按预期工作,但我是新手,我担心我错过了什么.据我所知,这是完整的.有时我觉得Xcode会挂起过去的错误,但也许我只是在失去理智!

谢谢!
-安德鲁

解决方法

在9.1.h中,你错过了’a’.

-(void) setReal: (double) andImaginary: (double) b;
//                       ^ here

代码仍然有效,因为在Objective-C中,选择器的部分可以没有名称,例如

-(id)initWithControlPoints:(float)c1x :(float)c1y :(float)c2x :(float)c2y
//                                    ^           ^           ^

这些方法称为

return [self initWithControlPoints:0.0f :0.0f :1.0f :1.0f];
//                                      ^     ^     ^

选择器名称自然是@selector(initWithControlPoints ::: :).

因此,编译器会将您的声明解释为

-(void)setReal:(double)andImaginary
              :(double)b;

由于你没有提供这个-setReal ::方法的实现,gcc会警告你

warning: incomplete implementation of class ‘Complex’
warning: method definition for ‘-setReal::’ not found

顺便说一句,如果你只是想要一个复杂的值,但不需要它是一个Objective-C类,那么就有C99 complex,例如

#include <complex.h>

...

double complex z = 5 + 6I;
double complex w = -4 + 2I;
z = z + w;
printf("%g + %gin",creal(z),cimag(z));

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读