// 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
On the final @end
line, Xcode is telling me the implementation is incomplete. The code still works as expected, but I'm new at this and am worried I've missed something. It is complete as far as I can tell. Sometimes I feel like Xcode hangs on to past errors, but maybe I'm just losing my mind!
Thanks! -Andrew
In 9.1.h
, you have missed an 'a'.
-(void) setReal: (double) andImaginary: (double) b;
// ^ here
The code is still valid, because in Objective-C a selector's part can have no name, e.g.
-(id)initWithControlPoints:(float)c1x :(float)c1y :(float)c2x :(float)c2y
// ^ ^ ^
these methods are called as
return [self initWithControlPoints:0.0f :0.0f :1.0f :1.0f];
// ^ ^ ^
and the selector name is naturally @selector(initWithControlPoints::::)
.
Therefore, the compiler will interpret your declaration as
-(void)setReal:(double)andImaginary
:(double)b;
since you have not provided the implementation of this -setReal::
method, gcc will warn you about
warning: incomplete implementation of class ‘Complex’
warning: method definition for ‘-setReal::’ not found
BTW, if you just want a complex value but doesn't need it to be an Objective-C class, there is C99 complex, e.g.
#include <complex.h>
...
double complex z = 5 + 6I;
double complex w = -4 + 2I;
z = z + w;
printf("%g + %gi\n", creal(z), cimag(z));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With