objective c - Class in ObjectiveC doesn't seem to recognize inherited messages -


i'm learning objectivec "programming in objective c" stephen g kochan , i'm having difficulties example. creates simple fraction class inherits object. that's trouble, when try send messages understood object instead of fraction, such 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 1/3     [myfraction setnumerator: 1];     [myfraction setdenominator: 3];       // display fraction     printf("the value of fraction is: ");     [myfraction print];     printf("\n");       // destroy instance     [myfraction free];       return 0; } 

when compile it: gcc fraction.m -o fraction -l objc

i following warnings:

fraction.m: in function ‘main’: fraction.m:47: warning: ‘fraction’ may not respond ‘+alloc’ fraction.m:47: warning: (messages without matching method signature fraction.m:47: warning: assumed return ‘id’ , accept fraction.m:47: warning: ‘...’ arguments.) fraction.m:48: warning: ‘fraction’ may not respond ‘+init’ fraction.m:62: warning: ‘fraction’ may not respond ‘-free’ fernando@mcfofo ~/code/learning objective c:: ./fraction  objc[1678]: fraction: not recognize selector forward:: 

and program, when run, complains illegal instruction…

anybody knows what's going on?

classes in cocoa (the popular objective-c library, synonymous) tend inherit nsobject, cocoa root class. documented here.

there nsobject protocol, implemented nsproxy. forms basis of cocoa's support distributed objects. said, may getting ahead of ourselves that!

i notice trying following:

fraction *myfraction; myfraction = [fraction alloc]; myfraction = [fraction init]; 

you allocating memory object , losing overwriting pointer one. standard practice on 1 line follows:

fraction *myfraction = [[fraction alloc] init]; 

if must 1 multiple lines, must following:

fraction *myfraction; myfraction = [fraction alloc]; myfraction = [myfraction init]; // myfraction ready go! 

i stumbled across site refers appears book you're reading. examples highlighted on website inherit nsobject , import <foundation/nsobject.h>. there particular reason eschewed , decided upon object class provided runtime?


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -