chapter 17 excersize 3

Discussion in 'Mac Programming' started by jamesapp, May 13, 2008.

  1. jamesapp macrumors 6502a

    Joined:
    Mar 7, 2008
    #1
    working on excersize 3 from a book on Objective-C by Stephen G. Kochan
    it is from chapter 17
    here is the excersize from the book:

    Code:
    3. Return to the Fraction class you worked with throughout Part I, "The Objective-C Language." For your convenience it is listed in Appendix D, "Fraction and Address Book Examples." Modify that class to work under the Foundation framework. Then add messages as appropriate to the various MathOps category methods to add the fractions resulting from each operation to the autorelease pool.
    When that is done, can you write a statement like this:
    [[fractionA add: fractionB] print];
    
    here is the answer to the excersize which i got from someone on this site

    Code:
    17-3
    Just add an autorelease message to the result fraction in each method
    before the return is made.  In the simplest way, this can be done by 
    changing the line
    
            return result;
    
    at the end of each method to
    
            return [result autorelease];
    
    given that autorelease returns the object (which I realize I overlooked
    mentioning that in the text...oh well, next printing!)
    
    And yes, expressions such as 
    
            [[fractionA  add: fractionB] print];
    
    could then be written without memory leakage because the fraction returned
    by the add: method would have been added to the autorelease pool and 
    therefore will get released when the pool is released.
    
    i tried to rewrite a program from an earlier chapter
    the program i rewrote, was prog11.2.m
    which i will include i renamed the program prog11.2b.m to keep it seperate from the earlier program, here is prog11.2b.m

    Code:
    #import "Fraction.h"
    
    @interface FractionB: Fraction
    -(void) print;
    @end
    
    @implementation FractionB;
    -(void) print
    {
      printf (" (%i/%i) ",numerator, denominator);
    }
    @end
    
    int main (int argc, char *argv[])
    {
      NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
      Fraction *a;
      Fraction *b;
      Fraction *result;
      
      [FractionB poseAsClass: [Fraction class]];
      
      a = [[Fraction alloc] init];
      b = [[Fraction alloc] init];
      
      [a setTo: 1 over: 3];
      [b setTo: 2 over: 5];
      
      [[fractionA  add: fractionB] print];
      
      [a print]; printf (" + "); [b print]; printf (" = ");
      result = [a add: b];
      [result print];
      printf ("\n");
      [a release];
      [b release];
      [result release];
      
      return 0;
    }
    
    what i want to do is add the expression
    Code:
      [[fractionA add: fractionB] print];
    
    to the program.
    just wondering about what i would add to the interface file and implementation file
    so i could put this expression into my test program?
    i added the above line to my test program in main and when i tried to compile it i got the following error message:

    james-collinss-macbook-pro:prog17 jamescollins$ gcc -framework Foundation Fraction.m prog11.2b.m -o prog11.2b
    prog11.2b.m: In function ‘main’:
    prog11.2b.m:29: error: ‘fractionA’ undeclared (first use in this function)
    prog11.2b.m:29: error: (Each undeclared identifier is reported only once
    prog11.2b.m:29: error: for each function it appears in.)
    prog11.2b.m:29: error: ‘fractionB’ undeclared (first use in this function)

    any help would be appreciated.
     
  2. darkwing macrumors 65816

    Joined:
    Jan 6, 2004
    #2
    You're trying to add the name of the classes and not the instances. Perhaps you meant:

    [[a add: b] print];
     

Share This Page