不完整的实现(xcode错误?)

时间:2011-05-18 18:01:01

标签: objective-c xcode

// 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会挂起过去的错误,但也许我只是在失去理智!

谢谢! -Andrew

1 个答案:

答案 0 :(得分:10)

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

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

#include <complex.h>

...

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