Amazon.com Widgets Build Fail Error
Welcome, Guest. Please login or register.
Did you miss your activation email?
June 18, 2013, 11:55:11 PM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  Programming in Objective-C, 4th edition
| |-+  Chapter 11
| | |-+  Build Fail Error
Pages: [1]   Go Down
Print
Author Topic: Build Fail Error  (Read 590 times)
Qpido
Newbie
*
Posts: 7






« on: May 02, 2012, 09:23:16 AM »

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:
Code: (Objective-C)
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
Code: (Objective-C)
//
//  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
Code: (Objective-C)

//
//  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
Code: (Objective-C)

//
//  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
« Last Edit: May 02, 2012, 09:50:20 AM by Qpido » Logged
Shatrov
Newbie
*
Posts: 1


Email




« Reply #1 on: August 05, 2012, 04:25:37 AM »

Thank you! This helped me a lot. Registered to say this.
Also, following is the code using the methods' names given in the book:

Code: (Objective-C)
//
//  main.m
//  FractionTest
//
//  Created by (me) on 05.08.12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "Fraction+MathOps.h"
#import "Complex.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 setTo:5 over:8];
        [b setTo:6 over:10];
       
        [a print]; NSLog(@" +"); [b print]; NSLog(@"-----");
        result = [a add:b];
        [result print];
        NSLog(@"\n");
       
        [a print]; NSLog(@" -"); [b print]; NSLog(@"-----");
        result = [a sub:b];
        [result print];
        NSLog(@"\n");
       
        [a print]; NSLog(@" *"); [b print]; NSLog(@"-----");
        result = [a mul:b];
        [result print];
        NSLog(@"\n");
       
        [a print]; NSLog(@" /"); [b print]; NSLog(@"-----");
        result = [a div:b];
        [result print];
        NSLog(@"\n");
    }
    return 0;
}
« Last Edit: August 05, 2012, 04:27:39 AM by Shatrov » Logged
Pages: [1]   Go Up
Print
Jump to:  



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.