Amazon.com Widgets Program 3.2 SIGABRT error message
Welcome, Guest. Please login or register.
Did you miss your activation email?
November 27, 2014, 12:25:53 PM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Old Stuff
| |-+ Chapter Study
| | |-+ Chapter 3 - Classes, Objects, and Methods
| | | |-+ Program 3.2 SIGABRT error message
Pages: [1] Go Down
Print
Author Topic: Program 3.2 SIGABRT error message (Read 2742 times)
pro11
Newbie
*
Posts: 7


Email




on: December 26, 2010, 12:56:56 AM

I'm getting this error message: GDB: program received signal: "SIGABRT."
Ive reviewed my code several times and cant find whats wrong with it.
I would really appreciate if you guys can point out my mistake.
Here's my code:
Code: (Objective-C)
//Program to work with fractions - class version
#import <Foundation/Foundation.h>

//---- @interface section ----

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

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

@end

// ---- @implementatioin section ----

@implementation Fraction
- (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[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Fraction *myFraction;

//Create an instance of a fraction

myFraction = [Fraction alloc];
myFraction = [Fraction init];

//Set fraction to 1/3

[myFraction setNumerator: 1];
[myFraction setDenominator: 3];

//Display the fraction using the print method

NSLog (@"The value of myFraction is:");
[myFraction print];
[myFraction release];

[pool drain];
return 0;
}

Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #1 on: December 26, 2010, 01:19:29 PM

The init method is an instance method, not a class method.  So you need to send that message to an instance from the class, which in this case is myFraction.

Cheers,

Steve
Logged
pro11
Newbie
*
Posts: 7


Email




Reply #2 on: December 27, 2010, 11:11:37 PM

That works, Im having a hard time understanding why but Im sure it will make sense later.

Thanks a lot Steve.
Logged
pro11
Newbie
*
Posts: 7


Email




Reply #3 on: December 28, 2010, 12:51:14 AM

Steve I have a question in regards to this chapter 3.2 program and in specific to this line.

Code: (Objective-C)
myFraction = [Fraction alloc]; 
myFraction = [myFraction init];

After reading the program section I would like to ask what is the difference or advantage, if theres any, to combine those two messages in the same line? Or is it just for coding style differences?

Code: (Objective-C)
myFraction = [[Fraction alloc] init];

Thanks!

Logged
Jake
Newbie
*
Posts: 14






Reply #4 on: December 28, 2010, 11:44:23 PM

Steve I have a question in regards to this chapter 3.2 program and in specific to this line.

Code: (Objective-C)
myFraction = [Fraction alloc]; 
myFraction = [myFraction init];

After reading the program section I would like to ask what is the difference or advantage, if theres any, to combine those two messages in the same line? Or is it just for coding style differences?

Code: (Objective-C)
myFraction = [[Fraction alloc] init];

Thanks!



It's does indeed give the same desire affect. However, it's often merge into one.
For example:

Code: (Objective-C)
@property int test1, test2;

and

Code: (Objective-C)
@property int test1;
@property int test2;

are exactly the same and give the same affect. However, isn't the second code a bit more to type? And so often, the alloc and init message are often merge into one.
Logged
pro11
Newbie
*
Posts: 7


Email




Reply #5 on: December 29, 2010, 10:34:20 PM

Thank you very much Jake!
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #6 on: December 30, 2010, 09:40:48 AM

Yeah, it's just easier to type.  The condensed version might be a bit more efficient as well, although that would likely be negligible.   Some might also argue that the compact version is easier to read (once you get used to it), as you can see the same object that being allocated is then being initialized.

Cheers,

Steve
Logged
Pages: [1] Go Up
Print
Jump to:



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.