Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
October 18, 2018, 04:18:54 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register Chat  
Pages: [1]   Go Down
  Print  
Author Topic: Build Fail Error  (Read 2788 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
andremaha
Newbie
*
Posts: 2


Email
« Reply #2 on: August 25, 2014, 07:59:51 AM »

As soon as I import the m file

// Fraction+MathOps.m
#import "Fraction.m"
#import "Fraction+MathOps.h"

 - this error occures:

ld: 8 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

But I DO need the Fraction.m - since there the reduce and other important methods are located.

When I only import the .h files in the Fraction+MathOps.m file, everything compiles fine, but I have the  problem, since my Category implementation "does not know" what I mean when I call [result reduce] or [self reduce].

What do I do?
Logged
andremaha
Newbie
*
Posts: 2


Email
« Reply #3 on: August 25, 2014, 08:38:11 AM »

So, I figured this one one.

First, no, it's not a good practice to include implementation .m files - only headers .h.
Second, the reason why I could not make the reduce method to work is simple. I experimented with the class extensions. And declared this method as class extension:

Code: (Objective-C)
// Fraction.m
// Class extension
@interface Fraction ()
-(void) reduce;
@end

That means that I removed the method definition from the header file:

Code: (Objective-C)

// Fraction.h

#import <Foundation/Foundation.h>

@interface Fraction: NSObject

@property int numerator, denominator;

-(void) print: (BOOL) reduce;
-(void) setTo: (int) n over: (int) d;
-(double) convertToNum;

@end

That is why, when I tried to #import Fraction.h - my Category's impementation would break. And when I included the Fraction.m compiler could find the method, but other problems appeared ( linker command failed with exit code 1 (use -v to see invocation))

Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

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 ゥ 2009 classroomM.com. All rights reserved.