Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

So I'm new to Objective-C and I'm following this tutorial. I'm running Linux Mint 14. I've installed gobjc by running sudo apt-get install gobjc and I have gcc already installed as well. I'm trying out their Point example, but I'm running into some weird errors.

Here's my code (pretty much copy and pasted from the website):

#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'm compiling using gcc Point.m -lobjc -lm.

Here's the error I'm getting:

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've looked up a lot about this problem, but I haven't been able to find much. Everything suggests switching to the newer GNUStep and NSObject, but I'm writing a program for one of my CS classes, and I think I have to stick to objc/Object.h.

At the beginning of the year, we were given a pre-configured Ubuntu image to use in VirtualBox that we could program on, and this program works fine on that. I'm not sure what is on there that makes it work. Could it be that Linux Mint 14 doesn't support this old of a version of Objective-C?

Any help/feedback is appreciated!

like image 899
justindao Avatar asked Apr 17 '13 15:04

justindao


1 Answers

Using Object is pretty archaic in Objective-C (goes back to before Next took over the development of the language).

Modern Objective-C is entangled with the Foundation framework (the non-UI part of Cocoa) through compiler support for:

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

Hence, learning Objective-C without Foundation doesn't make much sense, in my very personal opinion.

You could include Foundation (available somehow on Linux, too) and make Point a subclass of NSObject. That should get you going.

like image 170
Monolo Avatar answered Nov 18 '22 01:11

Monolo