Objective C, first program, compilation issues

Discussion in 'Mac Programming' started by Alla, Nov 10, 2015.

  1. Alla, Nov 10, 2015
    Last edited by a moderator: Nov 10, 2015

    Alla macrumors newbie

    Alla

    Joined:
    Nov 10, 2015
    #1
    Hello!

    I am new to this forum, and new to programming, although I have already reached chapter 18
    of Programming in C textbook.

    Please, take a look at the program I typed in from the book, and warning messages I receive.
    I would be grateful for explanation on what I am doing wrong. I work on Mac OS 10.7.5, Xcode 4.6.3,
    Terminal 2.2.3.
    1) I created a file with the command:
    [touch prog18-2.m]

    2) Program

    Code:
    // Program to work with fractions - Objective-C version
    #import <stdio.h>
    #import <objc/Object.h>
    
    //------ @interface section ---------
    
    @interface Fraction: Object
    {
    int numerator;
    int denominator;
    }
    -(void) set_numerator: (int) n;
    -(void) set_denominator: (int) d;
    -(void) print;
    
    @end
    
    // ----- @implementation section -------
    
    @implementation Fraction;
    
    // getters
    -(int) numerator
    {
    return numerator;
    }
    
    -(int) denominator
    {
    return denominator;
    }
    
    //setters
    -(void) set_numerator: (int) num
    {
    numerator = num;
    }
    
    -(void) set_denominator: (int) denom
    {
    denominator = denom;
    }
    
    //other
    -(void) print
    {
    printf("The value of the fraction is %i/%i\n", numerator, denominator);
    }
    
    @end
    
    //-------- program section -----------
    
    int main(void)
    {
    Fraction* my_fract;
    my_fract = [Fraction new];
    
    [my_fract set_numerator: 1];
    [my_fract set_denominator: 3];
    
    printf("The numerator is %i, and teh denominator is %i\n", [my_fract numerator], [my_fract denominator]);
    [my_fract print];
    
    [my_fract free]; //frees the memory that was used by Fraction object
    
    return 0;
    }
    3) I compiled it with:
    Code:
    gcc -framework Foundation prog18-2.m -o prog18-2
    4) Terminal generated the following:
    Code:
    prog18-2.m: In function ‘main’:
    prog18-2.m:58: warning: ‘Fraction’ may not respond to ‘+new’
    prog18-2.m:58: warning: (Messages without a matching method signature
    prog18-2.m:58: warning: will be assumed to return ‘id’ and accept
    prog18-2.m:58: warning: ‘...’ as arguments.)
    prog18-2.m:71: warning: ‘Fraction’ may not respond to ‘-free’
    Thank you!
     
  2. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    I haven't used Obj-C in a long time, but I thought you normally create objects with [[Class alloc] init], not [Class new], and that you get rid of them with [instance release], not [instance free].
     
  3. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #3
    As far as I'm aware, [Class new] is the same as [[Class alloc] init]. Wouldn't hurt to try [[Class alloc] init] instead, since the 'new' method seems to be causing problems for some reason.
     
  4. Senor Cuete macrumors regular

    Joined:
    Nov 9, 2011
    #4
    the '+' in '+new' is probably complaining that you are calling a class method rather than instance method. A method can be either. Step through your code with the debugger and see if the object is allocated. In XCode I can create an instance of an object with new, but try [[object alloc] init] instead.

    free is a C function. Try [object dealloc] instead. This might be useful:

    https://developer.apple.com/library...roduction.html#//apple_ref/doc/uid/TP40011210
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    Which book, specifically, are you referring to? Exact title, author, and edition would be helpful. Thanks.
     
  6. cqexbesd macrumors regular

    Joined:
    Jun 4, 2009
    #6
    Is "new" available from Object? I thought it was only from NSObject? I don't have a mac handy but on my environment using only libobjc2 from gnustep there is no "new".
     
  7. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #7
    First, if you have Xcode, why are you compiling using the Terminal command line?
    If you make an Xcode project correctly, it should automatically use the right compiler, foundations, headers, etc.

    Second, I looked at several versions of the OS X objc/Object.h header, and they all have +new and -free methods declared. This suggests the correct header is not being included. There could be several reasons for this. The starting point to discover whether this is the cause of the problem is to identify exactly which header is being included.

    The man page for 'gcc' shows the -v and -H options may be useful here. The -H option in particular will tell you the full pathname of every header included. Please post that complete output here.

    If the objc/Object.h header that's actually included doesn't reside in /usr/include, then your 'gcc' is somehow configured to use include files other than the OS X standard ones. The -v option should show you the final form of the include path. Please post that output here.

    As cqexbesd noted, there is no 'new' method in the gnustep version. So if you're somehow using those headers, it would explain the result you're seeing.
     
  8. Senor Cuete macrumors regular

    Joined:
    Nov 9, 2011
    #8
    I second the advice to use an XCode project rather than compiling with the terminal. XCode gives you a lot, such as a stepping through your code with the debugger and looking a the values of your variables.
     
  9. Alla, Nov 10, 2015
    Last edited by a moderator: Nov 11, 2015

    Alla thread starter macrumors newbie

    Alla

    Joined:
    Nov 10, 2015
    #9
    Thank you, I will follow your advice, except for debugger - I don't know yet how to work with it.

    Thank you! Well, I use Terminal because I have not yet used Xcode to create projects, I haven't learned it yet.
     
  10. Alla thread starter macrumors newbie

    Alla

    Joined:
    Nov 10, 2015
    #10
    Sorry, I should've named it - Programming in C by Kochan
     
  11. Senor Cuete macrumors regular

    Joined:
    Nov 9, 2011
    #12
    For learning to program the Mac with OS X, XCode. Objective C / Cocoa and Interface Builder, I recommend this book:

    Cocoa Programming for Mac OS X - fifth edition:

    https://www.bignerdranch.com/we-write/cocoa-programming/

    This will tell you how to create a Mac application using XCode, including the rules for memory management. Basically the rules for memory management are:

    If you create an object using [[object alloc] init] or [object new] you are responsible for disposing of it. This will generally be done using [object release] or [object autorelease] NOT [object dealloc] because if you dealloc it explicitly it may be retained somewhere else in you program and the object retaining it will have a pointer to an invalid object. NSObjects have a dealloc method. You need to override the parent class' dealloc method with a method like this:
    Code:
    - (void) dealloc
    {
      //release anything the object has retained, derigister it as an observer, etc. and then:
       [super dealloc];
    }
    The book discusses this and also the use of Automatic Reference Counting instead of manual memory management. Most memory management is probably done automatically with ARC today but understanding how it works is a good idea.
     

Share This Page