0

为什么它说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;  
}  

分数.h

    @interface Fraction : NSObject
@property int numerator, denominator, counter;

//methods
@end

分数.m

@implementation
@synthesize numerator, denominator, counter;

//methods

@end
4

3 回答 3

1

你可能想检查你的分数类文件.h,检查你是否有一个分子和分母的集合属性。

@property(nonatomic, ...) Type *numerator
@property(nonatomic, ...) Type *denominator

而且在你的 .m 文件中,应该有

@synthesize numerator, denominator

或者,在您的 xcode 中,按住 Command 键的同时将鼠标指向该分子或分母,然后单击变量,xcode 会将您带到声明变量的文件,如果未声明,则不会发生任何事情。

于 2012-06-26T12:33:20.953 回答
0

尝试self.在代码中添加前缀:

result.numerator = (self.numerator * f.denominator)+(self.denominator * f.numerator);
于 2012-06-26T13:01:28.007 回答
0

看起来您正在使用分子和分母,而之前没有任何变量设置。您可能会对这些变量与其他变量具有相同名称这一事实感到困惑,但如果它们与调用它们的文件没有关联,那么您的程序将无法识别它们。您的 synthesize 语句可能需要移到您的 @implementation 语句下方。我对您显示的代码不包含分子和分母的声明这一事实感到有些困惑,我相信您的编译器也是如此。

于 2012-06-26T13:50:26.150 回答