Dear forum members,
I was finally able to make an account after several futile attempts, so here is my first post about a problem I'm encountering in Chapter 11.
UPDATE:
I solved it. The following things have to be made sure of:
1) When using the reference to the class instance variables in categories, you have to use the "self." tag in front of it.
2) Split the .h and the .m file for the categories.
The error I get is this:
linker command failed with exit code 1 (use -v to see invocation)
ld: duplicate symbol _OBJC_IVAR_$_Fraction.numerator in /Users/jeromegarot/Library/Developer/Xcode/DerivedData/Chapter_11-dowaiwpljpzrymhduvkplnmolwlo/Build/Intermediates/Chapter 11.build/Debug/Chapter 11.build/Objects-normal/x86_64/Fraction.o and /Users/jeromegarot/Library/Developer/Xcode/DerivedData/Chapter_11-dowaiwpljpzrymhduvkplnmolwlo/Build/Intermediates/Chapter 11.build/Debug/Chapter 11.build/Objects-normal/x86_64/main.o for architecture x86_64
I don't know quite how to read that, so here are the files I think are causing the problem.
Fraction+MathOps.h
//
// MathOps.h
// Chapter 11
//
// Created by Jerome Garot on 5/2/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "Fraction.h"
#import "Fraction.m"
@interface Fraction (MathOps)
-(Fraction *) add: (Fraction *) f;
-(Fraction *) subtract: (Fraction *) f;
-(Fraction *) multiply: (Fraction *) f;
-(Fraction *) divide: (Fraction *) f;
@end
@implementation Fraction (MathOps)
-(Fraction *) add: (Fraction *) f
{
Fraction *result = [[Fraction alloc] init];
result.numerator = (self.numerator * f.denominator) + (self.denominator * f.numerator);
result.denominator = self.denominator * f.denominator;
return result;
}
-(Fraction *) subtract: (Fraction *) f
{
Fraction *result = [[Fraction alloc] init];
result.numerator = (self.numerator * f.denominator) - (self.denominator * f.numerator);
result.denominator = self.denominator * f.denominator;
return result;
}
-(Fraction *) multiply: (Fraction *) f
{
Fraction *result = [[Fraction alloc] init];
result.numerator = self.numerator * f.numerator;
result.denominator = self.denominator * f.denominator;
return result;
}
-(Fraction *) divide: (Fraction *) f
{
Fraction *result = [[Fraction alloc] init];
result.numerator = self.numerator * f.denominator;
result.denominator = self.denominator * f.numerator;
return result;
}
@end
My main.m
//
// main.m
// Chapter 11
//
// Created by Jerome Garot on 5/2/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Fraction.h"
#import "Fraction+MathOps.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
Fraction *a = [[Fraction alloc] init];
Fraction *b = [[Fraction alloc] init];
Fraction *result = [[Fraction alloc] init];
[a setNum:5 withDenom:8];
[b setNum:6 withDenom:10];
[a print]; NSLog(@" +"); [b print];
result = [a add:b];
[result print];
NSLog(@"\n");
[a print]; NSLog(@" -"); [b print];
result = [a subtract:b];
[result print];
NSLog(@"\n");
[a print]; NSLog(@" *"); [b print];
result = [a multiply:b];
[result print];
NSLog(@"\n");
[a print]; NSLog(@" /"); [b print];
result = [a divide:b];
[result print];
NSLog(@"\n");
}
return 0;
}
My Fraction.m
//
// Fraction.m
// Chapter 11
//
// Created by Jerome Garot on 5/2/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "Fraction.h"
@implementation Fraction
@synthesize numerator, denominator;
-(void) setNum:(int)n withDenom:(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;
}
-(void) print
{
NSLog(@"%i/%i", numerator, denominator);
}
-(double) convertToNum
{
if ( denominator != 0)
return (double) numerator / denominator;
else
return NAN;
}
@end
Hope you guys can help me out with this one, I don't know where I went wrong in this.
Thanks in advance,
Q