来自Objective-C编程的多态性示例的问题,Stephen Kochan,第6版(程序9.1)

时间:2014-02-23 02:05:13

标签: objective-c polymorphism

**更新** 我已经在reduce方法中通过零错误更改了无关除。这不会导致问题。

原始问题文本 我正在阅读Stephen Kochan的书“Objective-C编程”第6版。 Capter 9(9.1)中有一个程序通过复数和分数来演示多态性,每个都有 print 方法和 add 方法。

resultComplex = [c1 add: c2]

resultFraction = [f1 add: f2]

和打印方法一样。这对我来说是完全可以理解的,但代码中似乎有一个半身像,我希望有人可以提供帮助。这本书非常出色,前面的章节以现有代码为基础。早期版本的代码都有效。我怀疑我做了一些愚蠢的事情,比如错误输入的东西,但我已经倾倒了几个小时。

输出如下:

Chapter_9[5617:303]  18 + 2.5i 
Chapter_9[5617:303]          +
Chapter_9[5617:303]  -5 + 3.2i 
Chapter_9[5617:303] -----------
Chapter_9[5617:303]  13 + 5.7i 
Chapter_9[5617:303] 
Chapter_9[5617:303] 1/10
Chapter_9[5617:303]    +
Chapter_9[5617:303] 2/15
Chapter_9[5617:303] ------

(lldb) 

该程序能够添加复数,但在添加分数时需要死亡。 Xcode将Fraction中的 添加 方法识别为攻击者,将下一行作为攻击点:

    Fraction *result = [[Fraction alloc] init];  // must be alloc/init here

我将为Fraction类包含类文件(.h和.m)。我遗漏了Complex类,因为代码的那部分运行正常,尽管在该类中有类似的行似乎工作正常。如果有必要,我会更新帖子以添加这些内容。如果我的错误很明显,我只是不想淹没每个人的代码。

更新 我正在插入调试和输出的图片

enter image description here

分数标题文件

//
//  Fraction.h
//  Chapter_9

#import <Foundation/Foundation.h>

@interface Fraction : NSObject

@property int numerator, denominator;

-(void)         print;
-(void)         setTo: (int) n over: (int)d;
-(double)       convertToNum;
-(Fraction *)   add: (Fraction *) f;
-(void)         reduce;

@end

分数实施文件

//
//  Fraction.m
//  Chapter_9


#import "Fraction.h"

@implementation Fraction

@synthesize numerator, denominator;

-(void) print
{
    NSLog (@"%i/%i", numerator, denominator);
}

-(double) convertToNum
    {
        if (denominator != 0)
            return (double) numerator / denominator;
        else
        return NAN;
    }

    -(void) setTo: (int) n over: (int) d
    {
    numerator = n;
    denominator = d;
}

-(void) reduce
{
    int u = numerator;
    int v= denominator;
    int temp;

    while (v!= 0) {
        temp = u % v;
        u = v;
        v = temp;

    numerator /= u;
    denominator /=u;  //originally there was an unrelated error here
    }
}


-(Fraction *) add: (Fraction *) f
{
    // To add two fractions:
    // a/b + c/d = ((a*d) + (b*c) / (b*d)
    // Store the answer in a new Fraction object called (result)
// ************* Here is where Xcode identifies the error *******************
    Fraction *result = [[Fraction alloc] init];  // must be alloc/init here


    result.numerator = numerator * f.denominator + denominator * f.numerator;


    result.denominator = denominator * f.denominator;

    [result reduce];

    return result;
}

@end

计划主文件:

//
//  main.m
//  Chapter_9

// Programming in Objective-C, Stephen Kochan, 6th Edition
//Problem 9.1: Shared Method Names: Polymorphism


#import "Fraction.h"
#import "Complex.h"

int main(int argc, const char * argv[])
{

    @autoreleasepool {

        Fraction *f1 = [[Fraction alloc] init];
        Fraction *f2 = [[Fraction alloc] init];
        Fraction *fracResult;

        Complex *c1 = [[Complex alloc] init];
        Complex *c2 = [[Complex alloc] init];
        Complex *compResult;

        [f1 setTo: 1 over: 10];
        [f2 setTo: 2 over: 15];


        [c1 setReal: 18 andImaginary:2.5];
        [c2 setReal: -5 andImaginary: 3.2];

        //add and print 2 complex numbers

        [c1 print]; NSLog(@"         +"); [c2 print];
        NSLog(@"-----------");
        compResult = [c1 add: c2];  //compResult is alloc/init in add method
        [compResult print];
        NSLog(@"\n");

        // add and print 2 fractions
        [f1 print]; NSLog(@"   +"); [f2 print];
        NSLog(@"------");

        //  ******************** Here is where the method call takes place that causes the error

        fracResult = [f1 add: f2]; //fracResult is alloc/init in add method
        [fracResult print];

    }
    return 0;
}

我显然是Xcode和Objective-C的新手,因此无法自行调试。很抱歉这个问题很长而且很简单。我希望有人可以提供帮助。

1 个答案:

答案 0 :(得分:2)

Xcode应该在reduce方法中为您提供相当清晰的除零错误。我猜你没有正确地从书中复制它,因为你的GCD算法错误导致了被零除错误。

此:

while (v!= 0) {
    temp = u % v;
    u = v;
    v = temp;

    numerator /= u;
    denominator /=v;
}

应该是这样的:

while (v!= 0) {
    temp = u % v;
    u = v;
    v = temp;
}
numerator /= u;
denominator /=u;