// 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
发布于 2011-05-18 18:04:27
在9.1.h中,你错过了一个'a‘。
-(void) setReal: (double) andImaginary: (double) b;
// ^ here代码仍然有效,因为在Objective中,选择器的部分不能有名称。
-(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顺便说一句,如果你只想要一个复杂的值,但不需要它是一个目标-C类,就会有C99配合物。
#include <complex.h>
...
double complex z = 5 + 6I;
double complex w = -4 + 2I;
z = z + w;
printf("%g + %gi\n", creal(z), cimag(z));https://stackoverflow.com/questions/6049033
复制相似问题