I'm learning ObjectiveC with "Programming in Objective C" by Stephen G Kochan and I'm having some difficulties with an example. It creates a simple Fraction class that inherits from Object. That's where I get into trouble, when I try to send messages that are understood by Object instead of Fraction, such as init, alloc or free (see code below):
// Fraction
#import <stdio.h>
#import <objc/Object.h> // base object
// @interface section
@interface Fraction: Object
{
int numerator;
int denominator;
}
- (void) print;
- (void) setNumerator: (int) n;
- (void) setDenominator: (int) d;
@end
// @implementation section
@implementation Fraction;
-(void) print
{
printf(" %i/%i ", numerator, denominator);
}
-(void) setNumerator: (int) n
{
numerator = n;
}
-(void) setDenominator: (int) d
{
denominator = d;
}
@end
// Program section
int main( int argc, char *argv[])
{
Fraction *myFraction;
// Create Instance of fraction
myFraction = [Fraction alloc];
myFraction = [Fraction init];
// Set fraction to 1/3
[myFraction setNumerator: 1];
[myFraction setDenominator: 3];
// Display the fraction
printf("The value of my fraction is: ");
[myFraction print];
printf("\n");
// Destroy the instance
[myFraction free];
return 0;
}
When I compile it:
gcc fraction.m -o fraction -l objc
I get the following warnings:
fraction.m: In function ‘main’:
fraction.m:47: warning: ‘Fraction’ may not respond to ‘+alloc’
fraction.m:47: warning: (Messages without a matching method signature
fraction.m:47: warning: will be assumed to return ‘id’ and accept
fraction.m:47: warning: ‘...’ as arguments.)
fraction.m:48: warning: ‘Fraction’ may not respond to ‘+init’
fraction.m:62: warning: ‘Fraction’ may not respond to ‘-free’
fernando@McFofo ~/code/learning objective c:: ./fraction
objc[1678]: Fraction: Does not recognize selector forward::
and the program, when run, complains about an illegal instruction…
Anybody knows what's going on?