Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MountainDev

macrumors newbie
Original poster
Oct 2, 2009
3
0
Anyone know why I am getting warnings on the following? The error seems to be coming from Fraction.M. I get two warning:

1. incomplete implemenation of class 'Fraction'
2. method definition for '-initwithNumerator:denominator:' not found.

Please help!


FRACTION.H

Code:
#import <Foundation/NSObject.h>

@interface Fraction: NSObject {
    int numerator;
    int denominator;
}

-(void) print;
-(Fraction*) initwithNumerator: (int) n denominator: (int) d;
-(void) setNumerator: (int) n andDenominator: (int) d;
-(int) numerator;
-(int) denominator;
@end

FRACTION.M

Code:
#import "Fraction.h"
#import <stdio.h>

@implementation Fraction
-(void) print {
    printf( "%i/%i", numerator, denominator );
}

-(Fraction*) initWithNumerator: (int) n denominator: (int) d {
    self = [super init];
	
	if ( self ) {
		[self setNumerator: n andDenominator: d];
	}
	
	return self;
}

-(void) setNumerator: (int) n andDenominator: (int) d {
	numerator = n;
	denominator = d;
}

//-(void) setNumerator: (int) n {
//	numerator = n;
//}

//-(void) setDenominator: (int) d {
//    denominator = d;
//}


-(int) numerator {
    return numerator;
}

-(int) denominator {
    return denominator;
}
@end

MAIN.M

Code:
#import <stdio.h>
#import "Fraction.h"

int main( int argc, const char *argv[] ) {
    // create a new instance
    Fraction *frac = [[Fraction alloc] initwithNumerator: 3 denominator: 7];
	
    // print it
    printf( "The fraction is: " );
    [frac print];
    printf( "\n" );
	
    // free memory
    [frac release];
	
    return 0;
}
 
Took me a while to spot, but method names are case sensitive.

Meaning initwithNumerator... is not the same as initWithNumerator...

You declared the later, but used the former in the main();


Thats one reason to love the Xcode's auto filling method name feature. Just start typing init and as long as xcode fills in the right one hit tab. I almost NEVER type in the full name for methods, in face almost never more than the first letter or two.

EDIT: Darn you Catfish_Man!!!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.