Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
August 17, 2017, 06:21:49 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: 7.2 Getting error messages.  (Read 1198 times)
HISWeb
Newbie
*
Posts: 1


Email
« on: January 13, 2012, 08:41:50 PM »

I'm trying to figure out what I'm doing wrong here.  I'm getting a bunch of error messages on my main.m telling me that I'm missing a right square bracket.  I've put !!!!! in front of the lines that are marked as an error.

Code: (Objective-C)
#import "Fraction.h"

int main (int argc, char * argv[])
{
    @autoreleasepool {
       
       
        Fraction *aFraction = [[Fraction alloc] init];
        Fraction *bFraction = [[Fraction alloc] init];
        Fraction *resultFraction;
       
        [aFraction setTo: 1 over: 4];
        [bFraction setTo: 1 over: 2];
       
       
       
        resultFraction = [aFraction add: bFraction];
        [resultFraction print: YES];
  !!!!!     
        resultFraction = [aFraction subtract: bFraction];
        NSLog (@"subtracted = ");
        [resultFraction print:YES];
!!!!!       
        resultFraction = [aFraction multiply: bFraction];
        NSLog (@"multiplied = ");
        [resultFraction print:YES];
!!!!!
       
        resultFraction = [aFraction divide: bFraction];
        NSLog (@"divided = ");
!!!!!        [resultFraction print: YES];
    }

       
   
    return 0;
}

and it's also saying I have incomplete implementation in my Fraction.m:

Code: (Objective-C)
#import "Fraction.h"

@implementation Fraction

@synthesize numerator, denominator;

-(void) print: (BOOL) d
{
    if (d==YES)
    {
        Fraction *result = [[Fraction alloc] init];
        result.numerator = numerator;
        result.denominator = denominator;
        [result reduce];
       
        NSLog(@"%i/%i", result.numerator, result.denominator);
    }
    else
        NSLog (@"%i/%i", numerator, denominator);
}


-(double) convertToNum
{
    if (denominator !=0)
        return (double) numerator / denominator;
    else
        return NAN;
}

-(void) setTo: (int) n over: (int) d
{   
    numerator = n;
    denominator = d;
}

-(Fraction *) add: (Fraction *) f
{
    Fraction *result = [[Fraction alloc] init];
    result.numerator = numerator * f.denominator + denominator * f.numerator;
    result.denominator = denominator * f.denominator;
   
   
   
   
    return result;
}

-(Fraction *) subtract: (Fraction *) f
{
    Fraction *result = [[Fraction alloc] init];
    result.numerator = numerator * f.denominator - denominator * f.numerator;
    result.denominator = denominator * f.denominator;
   
   
    return result;
}

-(Fraction *) multiply: (Fraction *) f
{
    Fraction *result = [[Fraction alloc] init];
    result.numerator = numerator * f.numerator;
    result.denominator = denominator * f.denominator;
   
   
    return result;
}

-(Fraction *) divide: (Fraction *) f
{
    Fraction *result = [[Fraction alloc] init];
    result.numerator = numerator / f.numerator;
    result.denominator = denominator / f.denominator;
   
   
    return result;
}

-(void) reduce
{
    int u = numerator;
    int v = denominator;
    int temp;
   
    while ( v!=0) {
        temp = u % v;
        u=v;
        v=temp;
    }
   
    numerator /= u;
    denominator /= u;
}
@end

I'm sure I'm missing something simple...can anyone help?
Logged
jgelling
Newbie
*
Posts: 27


« Reply #1 on: January 14, 2012, 12:25:40 AM »

I'm not sure why you're passing a BOOL to your Fraction's print method, presumably to toggle on reduction, although in that case the parameter should have a more descriptive name than "d" - by convention you use silly long names in Objective C - e.g. (BOOL) showReducedFractionResult - for parameters to make it absolutely clear what the method and parameter does.

Style aside, since print: (BOOL) d wasn't the example implementation in the book as far as I know, check your declarations. Given the errors you're describing and the complaints about an incomplete implementation, make sure you declare -(void) print: (BOOL) d, or some more descriptive parameter name than "d", in your Fraction.h instead of just -(void) print, otherwise Fraction.m has no idea what to do with that method.

Of course, -(void) print is an entirely different method than -(void) print (BOOL) doWhatever, and your declaration must match your implementation. That'd be my best guess where your problem lies, but without seeing your header file it's hard to tell. Hope that helps.
« Last Edit: January 14, 2012, 01:37:41 AM by jgelling » 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.