-1

Why ObjC program getting stuck? I'm newbie in ObjC and trying to compile it on Linux system by command:

gcc $(gnustep-config --objc-flags) *.m $(gnustep-config --base-libs)

Here the code:

int main (int argc, const char * argv[])
{
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

        Rect * r = [[Rect alloc]initWithWidth:30 height:50];
        NSLog(@"%f", [r height]);

        [pool drain];
        return 0;
}

//=====
#import <Foundation/Foundation.h>

@interface Rect : NSObject <NSCopying> {
}

@property double width;
@property double height;

- (Rect *) initWithWidth:(double)w
                  height:(double)h;
- (double) height;
//...

//=====
@implementation Rect
- (Rect *) initWithWidth:(double)w
                  height:(double)h
{
    self.width = w;
    self.height = h;
    return [super init];
}

- (double) height
{
    return self.height;
}
// ...
drewpts
  • 446
  • 1
  • 5
  • 20

1 Answers1

1

You are creating a infinite recursive loop when you call return self.height; change that line to return _height; and it should work. You can also remove the - (double) height; from the header file since your property declaration generates a setter - (void)setHeight:(double)height; and a getter - (double)height; automatically. You should also probably remove the getter from your implementation since its already generated with the property declaration.

Peter Segerblom
  • 2,773
  • 1
  • 19
  • 24
  • Thanks. I removed setterd and getters and got such error: *rectangle.m:29:1: warning: incomplete implementation of class ‘Rect’ rectangle.m:29:1: warning: method definition for ‘-setWidth:’ not found rectangle.m:29:1: warning: method definition for ‘-width’ not found rectangle.m:29:1: warning: method definition for ‘-setHeight:’ not found rectangle.m:29:1: warning: method definition for ‘-height’ not found* – drewpts Jul 29 '15 at 09:21
  • 2
    Since you are using gcc I'm not sure, whether it supports automatic accessor synthesize. Did you try to add `@synthesize height, width;` at the beginning of the implementation? However, you can use clang for compiling. – Amin Negm-Awad Jul 29 '15 at 09:28
  • Does the program compile and run ok? – Peter Segerblom Jul 29 '15 at 09:31
  • @PeterSegerblom no. I added ```@synthesize``` directive (just below under ```@implementation```), removed all setters and getters and anyway got errors about incomplete class declaration. Moreover I haven't used clang with other languages due to this bug with my distro: [link](http://stackoverflow.com/questions/26333823). I'll try to workaround this and will send more info. Thanks for your help, guys! – drewpts Jul 29 '15 at 09:39