Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

jamesapp

macrumors 6502a
Original poster
Mar 7, 2008
544
0
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.
 

darkwing

macrumors 65816
Jan 6, 2004
1,210
0
You're trying to add the name of the classes and not the instances. Perhaps you meant:

[[a add: b] print];
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.