Thanks for the suggestions. I think the book is Pre Obj 2.0 so that's why he's having us use Object and not NSObject. I made the change and still have problems. Here is my code now:
Code:
#import <objc/Object.h>
@interface Pointxy: NSObject
{
int x;
int y;
}
-(void) setX: (int) xVal;
-(void) setY: (int) yVal;
-(void) setX: (int) xVal andY: (int) yVal;
-(int) x;
-(int) y;
@end
#import "Pointxy.h"
@implementation Pointxy;
-(void) setX: (int) xVal
{
x = xVal;
}
-(void) setY: (int) yVal
{
y = yVal;
}
-(void) setX: (int) xVal andY: (int) yVal
{
x = xVal;
y = yVal;
}
-(int) x
{
return x;
}
-(int) y
{
return y;
}
@end
#import <objc/Object.h>
#import "Pointxy.h"
@interface Rectangle: NSObject
{
int width;
int height;
Pointxy *origin;
}
-(void) setWidth: (int) w;
-(void) setHeigh: (int) h;
-(void) setWidth: (int) andHeight: (int) h;
-(void) setOrigin: (Pointxy *) pt;
-(Pointxy *) origin;
-(int) width;
-(int) height;
-(int) area;
-(int) perimeter;
@end
#import "Rectangle.h"
@implementation Rectangle;
-(void) setWidth: (int) w
{
width = w;
}
-(void) setHeight: (int) h
{
height = h;
}
-(void) setWidth: (int) w andHeight: (int) h
{
width = w;
height = h;
}
-(int) width
{
return width;
}
-(int) height
{
return height;
}
-(int) area
{
return (width * height);
}
-(int) perimeter
{
return (2 * width + 2 * height);
}
-(void) setOrigin: (Pointxy *) pt
{
origin = [[Pointxy alloc] init];
[origin setX: [pt x] andy: [pt y]];
//origin = pt;
}
-(Pointxy *) origin
{
return origin;
}
@end
#import "Rectangle.h"
#import "Pointxy.h"
#import <stdio.h>
int main (int agrc, char *agrv[])
{
Rectangle *myRect = [[Rectangle alloc] init];
Pointxy *myPoint = [[Pointxy alloc] init];
[myPoint setX: 100 andY: 200];
[myRect setWidth: 5 andHeight: 8];
[myRect setOrigin: myPoint];
printf ("Rectangle w = %i, h = %i\n",
[myRect width], [myRect height]);
printf ("Origin at (%i, %i)\n",
[[myRect origin] x], [[myRect origin] y]);
printf ("Area = %i, Perimeter = %i\n",
[myRect area], [myRect perimeter]);
[[myRect origin] free];
[myRect free];
[myPoint free];
}
I have the same errors and the following warnings:
/Users/Sean/Developer/Objective-C/Rectangle.m:44: warning: 'Pointxy' may not respond to '-setX:andy:'
/Users/Sean/Developer/Objective-C/Rectangle.m:53: warning: incomplete implementation of class 'Rectangle'
/Users/Sean/Developer/Objective-C/Rectangle.m:53: warning: method definition for '-setWidth::' not found
/Users/Sean/Developer/Objective-C/Rectangle.m:53: warning: method definition for '-setHeigh:' not found
/Users/Sean/Developer/Objective-C/Program 8.4.m:12: warning: 'Rectangle' may not respond to '-setWidth:andHeight:'
/Users/Sean/Developer/Objective-C/Program 8.4.m:25: warning: 'Rectangle' may not respond to '-free'
/Users/Sean/Developer/Objective-C/Program 8.4.m:26: warning: 'Pointxy' may not respond to '-free'
And here are the error messages:
2008-08-06 11:09:30.685 Objective-C[4756:10b] *** _NSAutoreleaseNoPool(): Object 0x5c09c40 of class NSCFString autoreleased with no pool in place - just leaking
Stack: (0x1f37cdf 0x1e44562 0x6201f1 0x62025d 0x6202b1 0x61e94c 0x61ea12)
2008-08-06 11:09:30.687 Objective-C[4756:10b] *** _NSAutoreleaseNoPool(): Object 0x5c0b920 of class NSCFString autoreleased with no pool in place - just leaking
Stack: (0x1f37cdf 0x1e44562 0x1e4f9ba 0x62027d 0x6202b1 0x61e94c 0x61ea12)
2008-08-06 11:09:30.688 Objective-C[4756:10b] *** -[Pointxy setX:andy:]: unrecognized selector sent to instance 0x5c09740
2008-08-06 11:09:30.688 Objective-C[4756:10b] *** _NSAutoreleaseNoPool(): Object 0x5c0af20 of class NSCFString autoreleased with no pool in place - just leaking
Stack: (0x1f37cdf 0x1e44562 0x6201f1 0x62025d 0x6202f1 0x61e94c 0x61ea12)
2008-08-06 11:09:30.689 Objective-C[4756:10b] *** _NSAutoreleaseNoPool(): Object 0x5c0bc90 of class NSCFString autoreleased with no pool in place - just leaking
Stack: (0x1f37cdf 0x1e44562 0x1e4f9ba 0x62027d 0x6202f1 0x61e94c 0x61ea12)
2008-08-06 11:09:30.689 Objective-C[4756:10b] *** _NSAutoreleaseNoPool(): Object 0x5c0bcc0 of class NSCFString autoreleased with no pool in place - just leaking
Stack: (0x1f37cdf 0x1e44562 0x1e4f9ba 0x620312 0x61e94c 0x61ea12)
2008-08-06 11:09:30.692 Objective-C[4756:10b] *** _NSAutoreleaseNoPool(): Object 0x5c097f0 of class NSException autoreleased with no pool in place - just leaking
Stack: (0x1f37cdf 0x1e44562 0x620342 0x61e94c 0x61ea12)
2008-08-06 11:09:30.693 Objective-C[4756:10b] *** _NSAutoreleaseNoPool(): Object 0x5c0b3d0 of class _NSCallStackArray autoreleased with no pool in place - just leaking
Stack: (0x1f37cdf 0x1e44562 0x1e9e3e8 0x61914b 0x3e50fb 0x62034a 0x61e94c 0x61ea12)
2008-08-06 11:09:30.693 Objective-C[4756:10b] *** _NSAutoreleaseNoPool(): Object 0x5c0c150 of class NSCFString autoreleased with no pool in place - just leaking
Stack: (0x1f37cdf 0x1e44562 0x1e4f9ba 0x6190ce 0x3e50fb 0x62034a 0x61e94c 0x61ea12)
2008-08-06 11:09:30.695 Objective-C[4756:10b] *** _NSAutoreleaseNoPool(): Object 0x5c0c050 of class NSCFData autoreleased with no pool in place - just leaking
Stack: (0x1f37cdf 0x1e44562 0x1e58c35 0x1e58811 0x6190e2 0x3e50fb 0x62034a 0x61e94c 0x61ea12)
2008-08-06 11:09:30.696 Objective-C[4756:10b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[Pointxy setX:andy:]: unrecognized selector sent to instance 0x5c09740'
2008-08-06 11:09:30.697 Objective-C[4756:10b] Stack: (
6394187,
4083963,
6423370,
6416716,
6416914
Looks like the creation of the origin instance is causing the problem. I would have thought I'd want to create it this way instead:
Code:
Pointxy *origin = [[Pointxy alloc] init];
[origin setX: [pt x] andy: [pt y]];
since I'd want to create it as a pointer. I can skip this program for now, but it's bugging me that I don't know why this isn't working. More importantly I need to understand how to do this down the road. Any ideas?