New for Objective-C. Receiving "may not respond to a" new "warning." Constructor leading to segfault

So, I'm new to Objective-C, and I follow this tutorial . I am running Linux Mint 14. I installed gobjc by running sudo apt-get install gobjc and I already have gcc installed. I am testing their Point example, but I am encountering some strange bugs.

Here is my code (copy and paste from the site):

 #import <objc/Object.h> #import <math.h> #import <stdio.h> @interface Point : Object { @private double x; double y; } - (id) x: (double) x_value; - (double) x; - (id) y: (double) y_value; - (double) y; - (double) magnitude; @end @implementation Point - (id) x: (double) x_value { x = x_value; return self; } - (double) x { return x; } - (id) y: (double) y_value { y = y_value; return self; } - (double) y { return y; } - (double) magnitude { return sqrt(x*x+y*y); } @end int main(void) { Point *point = [Point new]; [point x:10.0]; [point y:12.0]; printf("The distance from the point (%g, %g) to the origin is %g.\n", [point x], [point y], [point magnitude]); return 0; } 

And I am compiling with gcc Point.m -lobjc -lm .

Here is the error I get:

 Point.m: In function 'main': Point.m:52:4: warning: 'Point' may not respond to '+new' [enabled by default] Point.m:52:4: warning: (Messages without a matching method signature [enabled by default] Point.m:52:4: warning: will be assumed to return 'id' and accept [enabled by default] Point.m:52:4: warning: '...' as arguments.) [enabled by default] 

It doesn't seem to be able to find the β€œnew” method (or maybe alloc / init?).

I searched a lot for this problem, but I could not find a lot. Everything suggests switching to the new GNUStep and NSObject , but I'm writing a program for one of my CS classes, and I think I should stick with objc/Object.h .

At the beginning of the year, we were provided with a pre-configured Ubuntu image for use in VirtualBox, on which we could program, and this program works fine. I am not sure what is there that makes it work. Can Linux Mint 14 not support this old version of Objective-C?

Any help / feedback is appreciated!

+6
source share
1 answer

Using Object pretty archaic in Objective-C (goes back to the fact that Next has started developing the language).

Modern Objective-C is confused with the Foundation base (a part other than Cocoa's UI), through compiler support for:

  • String and numeric literals
  • Collections (arrays and dictionaries),
  • Fast enumeration (for-in loops)
  • Memory Management (ARC and autorelease pools)

Therefore, learning Objective-C without Foundation does not make much sense, in my personal opinion.

You can enable Foundation (also available on Linux) and make Point a subclass of NSObject . That should make you go.

+6
source

All Articles