n00b just learning obj-c problem

Discussion in 'Mac Programming' started by Mykel2727, Oct 27, 2008.

  1. Mykel2727 macrumors newbie

    Jun 5, 2008
    my code looks like this:

    //  main.m
    //  prog2
    //  Created by MB on 10/24/08.
    //  Copyright TopTechPro 2008. All rights reserved.
    #import <stdio.h>
    #import <objc/Object.h>
    @interface Fraction: Object
    	int numerator; 
    	int denominator;
    -(void) print;
    -(void) setNumerator: (int) n;
    -(void) setDenominator: (int) d;
    @implementation Fraction;
    -(void) print
    	printf (" %i/%i ", numerator, denominator);
    -(void) setNumerator: (int) n
    	numerator = n;
    -(void) setDenominator: (int) d
    	denominator = d;
    //------------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:

    [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.
    what do i do?:confused:
  2. kpua macrumors 6502

    Jul 25, 2006
    Change this:

    myFraction = [Fraction alloc];
    myFraction = [Fraction init];
    to this:

    myFraction = [[Fraction alloc] init];
    Initializers are instance methods, not class methods.
  3. garethlewis2 macrumors 6502

    Dec 6, 2006
    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];
  4. gnasher729 macrumors P6


    Nov 25, 2005
    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.
  5. Mykel2727 thread starter macrumors newbie

    Jun 5, 2008

    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


Share This Page