obj-c compiler error help

Discussion in 'Mac Programming' started by uaecasher, Aug 19, 2009.

  1. uaecasher macrumors 65816

    uaecasher

    Joined:
    Jan 29, 2009
    Location:
    Stillwater, OK
    #1
    hello,

    I'm learning Obj-C and i reached where it teach me how to add Methods without argument names so here is my implementation section code:

    Code:
    //
    //  Fraction.m
    //  FractionTest
    //
    //  Created by Fahad Ali on 12/08/2009.
    //  Copyright 2009 __MyCompanyName__. All rights reserved.
    //
    
    #import "Fraction.h"
    
    
    @implementation Fraction
    
    -(void) print {
    	NSLog (@"%i/%i", numerator, denominator); 
    }
    
    -(double) convertToNum {
    	if (denominator != 0) return (double) numerator / denominator;
    	else return 1.0;
    }
    
    -(void) setTo: (int) n over: (int) d
    {
    	numerator = n;
    	denominator = d;
    	
    }
    - (void) add: (Fraction *) f		
    
    {
    	// To add two fractions: // a/b + c/d = ((a*d) + (b*c)) / (b * d)
    	numerator = numerator * f.denominator + denominator * f.numerator;
    	denominator = denominator * f.denominator;
    }
    
    @end
    
    the error is:

    error: requests for member 'denominator' in something is not a structure or union
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Where is the header file? Have you declared denominatoras a property?
     
  3. uaecasher thread starter macrumors 65816

    uaecasher

    Joined:
    Jan 29, 2009
    Location:
    Stillwater, OK
    #3
    Code:
    //
    //  Fraction.h
    //  FractionTest
    //
    //  Created by Fahad Ali on 12/08/2009.
    //  Copyright 2009 __MyCompanyName__. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    
    @interface Fraction : NSObject {
    	
    	int numerator;
    	int denominator;
    
    }
    -(void)		print;
    -(void)		setTo: (int) n over: (int) d;
    -(void)		add: (Fraction *) f;
    -(double)	convertToNum;
    
    @end
    
    
     
  4. MrFusion macrumors 6502a

    Joined:
    Jun 8, 2005
    Location:
    West-Europe
    #4
    [CODE

    @interface Fraction : NSObject {

    int numerator;
    int denominator;

    }

    @property int denominator;
    @property int numerator;

    -(void) print;
    -(void) setTo: (int) n over: (int) d;
    -(void) add: (Fraction *) f;
    -(double) convertToNum;

    @end


    @implementaion Fraction

    @synthesize denominator;
    @synthesize numerator;

    ....

    @end
    [/CODE]

    Try this.
     

Share This Page