Wednesday, 20 March 2013

Objective-c help


this first post on here , want start off saying hi! onto problem...

below code put xcode 4.2 on 32-bit macbook running snow leopard:

code:
#import <foundation/foundation.h>    // ---- @interface section ----    @interface fraction: nsobject    -(void) print;  -(void) setnumerator: (int) n;  -(void) setdenominator: (int) d;    @end    // ---- @implementation section ----    @implementation fraction  {      int numerator;      int denominator;  }    -(void) print  {      nslog(@"%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[])  {      @autoreleasepool {                    fraction *frac1 = [[fraction alloc] init];          fraction *frac2 = [[fraction alloc] init];                    // set 1st fraction 2/3                    [frac1 setnumerator: 2];          [frac1 setdenominator: 3];                    //set 2nd fraction 3/7                    [frac2 setnumerator: 3];          [frac2 setdenominator: 7];                    //display fractions                    nslog (@"first fraction is:");          [frac1 print];                    nslog (@"second fraction is:");          [frac2 print];                     //issue here last values in setnumerator/setdenominator overwrite all.            }      return 0;  }
when trying build/run, lot of errors (semantic errors, parse error). messed around code, took out curly braces around member declarations under @implementation , worked (sort of). although program ran, output:

first fraction 3/7
second fraction 3/7

can shed little light on why is? getting feeling program overwriting whatever stored frac1.

thanks!
 

first, you've typed in or copied code wrong. red-hilited piece wrong you've shown it. needs under @interface, so:

code:
@interface fraction: nsobject  {      int numerator;      int denominator;  }    -(void) print;  -(void) setnumerator: (int) n;  -(void) setdenominator: (int) d;    @end  
you should double-check you've typed in against whatever book you're using. programming requires accuracy. can't move pieces around arbitrarily.


second, if messed around braces, should post code, not code didn't compile. otherwise have guess code produced posted output. given posted code won't compile, there many ways have changed compile. we're left guess code was, in addition guessing why code produced results did.


finally, if you're learning book, it's idea post title, author, , edition number, know posted code coming from. can guess book is, it's more accurate if tell us.
 


Forums Macs Mac Programming


  • iPhone
  • Mac OS & System Software
  • iPad
  • Apple Watch
  • Notebooks
  • iTunes
  • Apple ID
  • iCloud
  • Desktop Computers
  • Apple Music
  • Professional Applications
  • iPod
  • iWork
  • Apple TV
  • iLife
  • Wireless

No comments:

Post a Comment