Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Mykel2727

macrumors newbie
Original poster
Jun 5, 2008
6
0
my code looks like this:

Code:
//
//  main.m
//  prog2
//
//  Created by MB on 10/24/08.
//  Copyright TopTechPro 2008. All rights reserved.
//

#import <stdio.h>
#import <objc/Object.h>

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

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

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

@end

//-----------@implementation----------------

@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 an instance of a Fraction

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

	//Set Faction to 1/3

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

	//Dsplay the fraction using print method

	printf ("The value of the fraction is:");
	[myFraction print];
	printf ("\n");
	[myFraction free];
	
	return 0;
}

In the console i get this error:

Code:
[Session started at 2008-10-27 22:46:22 -0700.]
2008-10-27 22:46:22.268 prog2[7909:10b] *** NSInvocation: warning: object 0x3020 of class 'Fraction' does not implement methodSignatureForSelector: -- trouble ahead
2008-10-27 22:46:22.269 prog2[7909:10b] *** NSInvocation: warning: object 0x3020 of class 'Fraction' does not implement doesNotRecognizeSelector: -- abort

[Session started at 2008-10-27 22:46:22 -0700.]
Loading program into debugger…
GNU gdb 6.3.50-20050815 (Apple version gdb-962) (Sat Jul 26 08:14:40 UTC 2008)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i386-apple-darwin".Program loaded.
sharedlibrary apply-load-rules all
Attaching to program: `/Users/mb/Desktop/prog2/build/Debug/prog2.app/Contents/MacOS/prog2', process 7909.
(gdb)

what do i do?:confused:
 

kpua

macrumors 6502
Jul 25, 2006
294
0
Change this:

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

to this:

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

Initializers are instance methods, not class methods.
 

garethlewis2

macrumors 6502
Dec 6, 2006
277
1
Your problem lies with the allocation of your fraction object.

myFraction = [Fraction alloc]; is correct

myFraction = [Fraction init]; is not correct.

alloc is known as a class method, init is an instance method. It should be written as

myFraction = [myFraction init];

Or the preferred shorthand is myFraction = [[Fraction alloc] init];
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,565
You are sending an "alloc" message to class Fraction. The class will respond by returning a pointer to an uninitialised object of type "Fraction". Now you are supposed to send an "init" message to the object of type "Fraction". Instead you send an "init" message to class "Fraction". The class "Fraction" is an object of type "Class".

If this succeeded, it would initialise the class "Fraction", not the object that you just created. And initializing the class "Fraction" is something that you absolutely don't want to do at this point.
 

Mykel2727

macrumors newbie
Original poster
Jun 5, 2008
6
0
thanks

thanks everyone for ur help
after this great macforums experience
i will be sure to come back with other (possibly dumb) questions i have while i learn obj-c

and as i get better i will be sure to help other n00bs out

this is my first major programming language (i know i little java -- not much)
and i am in high school learning this in my spare time

thanks
Mykel
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.