カテゴリMathOpsの分子と分母が宣言されていない変数であると表示されるのはなぜですか?
それは第11章とプログラム11.1にあります
間違いなく本と同じコードなので、なぜ動かないのか理解できません。
助けてください
コード:
#import <Foundation/Foundation.h>
#import "complex.h"
#import "Fraction.h"
#import "XYPoint.h"
@interface Fraction (MathOps)
-(Fraction *)add:(Fraction *) f;
-(Fraction *)mul:(Fraction *) f;
-(Fraction *)sub:(Fraction *) f;
-(Fraction *)div:(Fraction *) f;
@end
@implementation Fraction (MathOps)
-(Fraction *)add:(Fraction *) f
{
Fraction *result = [[Fraction alloc]init];
result.numerator = (numerator * f.denominator)+(denominator * f.numerator);
// numerator and denominator: Using undeclared variables.
result.denominator = denominator * f.denominator;
}
-(Fraction *)mul:(Fraction *) f
{
Fraction *result = [[Fraction alloc]init];
result.numerator = numerator * f.numerator;
result.denominator = denominator * f.denominator;
// numerator and denominator: Using undeclared variables.
}
-(Fraction *)sub:(Fraction *) f {}
-(Fraction *)div:(Fraction *) f {}
@end
typedef Fraction *FractionObj;
int main (int argc, const char * argv[])
{
@autoreleasepool {
complex *imaginary= [[complex alloc]init];
complex *real = [[complex alloc]init];
complex *results;
[real setReal:1];
[real setImaginary:2];
[real print];
[imaginary setReal:2];
[imaginary setImaginary:1];
[imaginary print];
results = [real add: imaginary];
[results print];
NSLog(@"FRACTIONS!");
Fraction *a = [[Fraction alloc]init];
Fraction *b = [[Fraction alloc]init];
id resultFraction;
[a setTo:20 over:3];
[b setTo:1 over:3];
[b minusSign];
[a minusSign];
id ab = a;
id ba = b;
[ab print: 0];
NSLog(@"-");
[ba print: 0];
NSLog(@"=");
resultFraction = [ab multiplyN: ba];
[resultFraction print: 1];
XYPoint *xy = [[XYPoint alloc]init];
[xy setX:100 andY:110];
[xy print];
NSLog(@"VERIFYING \n \n \n");
Fraction *fraction = [[Fraction alloc]init];
complex *Complex = [[complex alloc]init];
id number = [[complex alloc]init];
if([fraction isMemberOfClass: [complex class]])
NSLog(@"Fraction is member of class complex");
if([Complex isMemberOfClass: [NSObject class]])
NSLog(@"complex is member of class NSObject");
if([Complex isKindOfClass: [NSObject class]])
NSLog(@"complex is kind of class NSObject");
if([fraction isKindOfClass: [Fraction class]])
NSLog(@"Fraction is kind of class Fraction");
if([fraction respondsToSelector: @selector(print)])
NSLog(@"Fraction respond to selector print");
if([Complex respondsToSelector: @selector(print)])
NSLog(@"Complex responds to print");
if([Fraction instancesRespondToSelector: @selector(print)])
NSLog(@"Fraction's instances respond to print");
if([number respondsToSelector: @selector (print)])
NSLog(@"number responds to print");
if([number isKindOfClass: [complex class]])
NSLog(@"number is kind of class complex");
if([[number class] respondsToSelector:@selector (alloc)])
NSLog(@"Number class responds to alloc");
if([a counter]!=1){ NSLog(@"%i times was invoked method multiplyN", a.counter); }
else { NSLog(@"Only %i time was invoked method multiplyN", a.counter);}
typedef enum {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday} Day;
}
return 0;
}
Fraction.h:
@interface Fraction : NSObject
@property int numerator, denominator, counter;
//methods
@end
Fraction.m
@implementation
@synthesize numerator, denominator, counter;
//methods
@end