PDA

View Full Version : Objective-c help




a13x612
Feb 6, 2012, 07:17 PM
This is my first post on here and i just want to start off by saying hi! Now onto my problem...

Below is the code that I put into Xcode 4.2 on my 32-bit macbook running snow leopard:

#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 to 2/3

[frac1 setNumerator: 2];
[frac1 setDenominator: 3];

//Set 2nd fraction to 3/7

[frac2 setNumerator: 3];
[frac2 setDenominator: 7];

//Display the fractions

NSLog (@"First fraction is:");
[frac1 print];

NSLog (@"Second fraction is:");
[frac2 print];

//Issue here is the last values in setNumerator/setDenominator overwrite all.

}
return 0;
}

When trying to build/run, I get a lot of errors (semantic errors, parse error). So I messed around with the code, took out the curly braces around the member declarations under @implementation and it worked (sort of). Although the program ran, This was the output:

First fraction is 3/7
Second fraction is 3/7

Can anyone shed a little light on why this is? I am getting the feeling that the program is just overwriting whatever is stored for frac1.

Thanks!



chown33
Feb 6, 2012, 08:13 PM
#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;
}
...

First, you've typed in or copied the code wrong. The red-hilited piece is simply wrong where you've shown it. It needs to be under the @interface, like so:

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

-(void) print;
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;

@end

You should always double-check what you've typed in against whatever book you're using. Programming requires accuracy. You can't move pieces around arbitrarily.


Second, if you messed around with braces, you should post that code, not the code that didn't compile. Otherwise we have to guess what the code was that produced your posted output. Given that the posted code won't compile, there are many ways you could have changed it to compile. So we're left to guess what your code was, in addition to guessing why that code produced the results it did.


Finally, if you're learning from a book, it's a good idea to post the title, author, and the edition number, so we know where your posted code is coming from. We can guess what the book is, but it's more accurate if you tell us.