Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3214304
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:02:42+00:00 2026-05-17T15:02:42+00:00

I’m learning ObjectiveC with Programming in Objective C by Stephen G Kochan and I’m

  • 0

I’m learning ObjectiveC with “Programming in Objective C” by Stephen G Kochan and I’m having some difficulties with an example. It creates a simple Fraction class that inherits from Object. That’s where I get into trouble, when I try to send messages that are understood by Object instead of Fraction, such as init, alloc or free (see code below):

// Fraction
#import <stdio.h>
#import <objc/Object.h> // base object

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

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

@end


// @implementation section

@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 Instance of fraction 
    myFraction = [Fraction alloc];
    myFraction = [Fraction init];

    // Set fraction to 1/3
    [myFraction setNumerator: 1];
    [myFraction setDenominator: 3];


    // Display the fraction
    printf("The value of my fraction is: ");
    [myFraction print];
    printf("\n");


    // Destroy the instance
    [myFraction free];


    return 0;
}

When I compile it:
gcc fraction.m -o fraction -l objc

I get the following warnings:

fraction.m: In function ‘main’:
fraction.m:47: warning: ‘Fraction’ may not respond to ‘+alloc’
fraction.m:47: warning: (Messages without a matching method signature
fraction.m:47: warning: will be assumed to return ‘id’ and accept
fraction.m:47: warning: ‘...’ as arguments.)
fraction.m:48: warning: ‘Fraction’ may not respond to ‘+init’
fraction.m:62: warning: ‘Fraction’ may not respond to ‘-free’
fernando@McFofo ~/code/learning objective c:: ./fraction 
objc[1678]: Fraction: Does not recognize selector forward::

and the program, when run, complains about an illegal instruction…

Anybody knows what’s going on?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T15:02:43+00:00Added an answer on May 17, 2026 at 3:02 pm

    Classes in Cocoa (the most popular Objective-C library, they are almost synonymous) tend to inherit from NSObject, the Cocoa root class. It is documented here.

    There is also the NSObject protocol, implemented by NSProxy. It forms the basis of Cocoa’s support for Distributed Objects. That said, we may be getting ahead of ourselves with that!

    I also notice that you are trying to do the following:

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

    You are allocating memory for your object and then losing it by overwriting the pointer with another one. The standard practice is to do it on one line as follows:

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

    If you must do it one multiple lines, you must do the following:

    Fraction *myFraction;
    myFraction = [Fraction alloc];
    myFraction = [myFraction init];
    // myFraction is now ready to go!
    

    I stumbled across a site that refers to what appears to be the book you’re reading. The examples highlighted on the website inherit from NSObject and import <Foundation/NSObject.h>. Is there any particular reason you eschewed this and decided upon the Object class provided by the runtime?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have text I am displaying in SIlverlight that is coming from a CMS
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.