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

#### Alla

##### macrumors newbie
Original poster
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!

Last edited by a moderator:

#### ArtOfWarfare

##### macrumors G3
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].

#### superscape

##### macrumors 6502a
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].

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.

#### Senor Cuete

##### macrumors 6502
prog18-2.m:58: warning: ‘Fraction’ may not respond to ‘+new’
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

#### dejo

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

Which book, specifically, are you referring to? Exact title, author, and edition would be helpful. Thanks.

#### cqexbesd

##### macrumors regular
prog18-2.m:58: warning: ‘Fraction’ may not respond to ‘+new’

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".

ArtOfWarfare

#### chown33

##### Moderator
Staff member
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.

superscape

#### Senor Cuete

##### macrumors 6502
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.

#### Alla

##### macrumors newbie
Original poster
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
Thank you, I will follow your advice, except for debugger - I don't know yet how to work with it.

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.

Thank you! Well, I use Terminal because I have not yet used Xcode to create projects, I haven't learned it yet.

Last edited by a moderator:

#### Alla

##### macrumors newbie
Original poster
Which book, specifically, are you referring to? Exact title, author, and edition would be helpful. Thanks.
Sorry, I should've named it - Programming in C by Kochan

#### Senor Cuete

##### macrumors 6502
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.

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