Probably a Simple Problem...

Discussion in 'Mac Programming' started by CaptainZap, Feb 10, 2007.

  1. CaptainZap macrumors regular

    Jan 17, 2007
    Well I am working my way through "Programming in Objective-C" book, and am currently stuck on an exercise in Chapter 8, unfortunately it is an even so I can't see what Kochan has on his site. So the question is,
    So in the Rectangle.h, I made the method declaration,
    -(Point *) translate: (Point *) vector;
    and it is supposed to take a Point instance and translate it by its x and y values.

    So I came up with this method definition...
    -(Point *) translate: (Point *) vector;
    	float vectorX, vectorY;
    	origin = [[Point alloc] init];
    	vectorX = [[vector origin] x] + x;
    	vectorY = [[vector origin] y] + y;
    	[origin setX: vectorX setY: vectorY];
    then in the main.m, I did this

    int main (int argc, char *argv[])
    	Rectangle *myRectangle = [[Rectangle alloc] init];
    	Point *myPoint = [[Point alloc] init];
    	Point *translation = [[Point alloc] init];
    	[myRectangle setHeight: 12];
    	[myRectangle setWidth: 3];
    	[myPoint setX: 6 setY: 7];
    	[translation setX: 4 setY: 3];
    	[myRectangle setOrigin: myPoint];
    	[myRectangle translate: translation];
    	printf ("Now the Origin is at (%g,%g)\n", [[myRectangle origin] x], [[myRectangle origin] y]);
    	return 0;
    I get the errors, error: 'x' undeclared (first use in this function) and the same for y, if you need the rest of the .m and .h files, just ask.
  2. bronxbomber92 macrumors regular

    Nov 23, 2006
    It would help to know what the structs/classes of Point and Rect contained :)
  3. CaptainZap thread starter macrumors regular

    Jan 17, 2007
    K, for Rectangle...
    @interface Rectangle: Object
    	float height;
    	float width;
    	Point *origin;
    -(void) setHeight: (float) h;
    -(void) setWidth: (float) w;
    -(float) height;
    -(float) width;
    -(float) area;
    -(float) perimeter;
    -(void) setOrigin: (Point *) pt;
    -(Point *) origin;
    -(Point *) translate: (Point *) vector;
    -(id) free;
    And for Point
    @interface Point: Object
    	float x;
    	float y;
    -(void) setX: (float) xVal;
    -(void) setY: (float) yVal;
    -(void) setX: (float) xVal setY: (float) yVal;
    -(float) x;
    -(float) y;
    P.S. I left out the preprocessor lines, so don't think that I forgot to put them in my program :p
  4. yoavcs macrumors regular

    Apr 7, 2004
    You aren't defining the 'x' and 'y' local variables you use in the translate: method anywhere...

    Edit for clarity: not the ones in the [[vector origin] x] message, the ones outside.
  5. CaptainZap thread starter macrumors regular

    Jan 17, 2007
    Ah, ok, I dunno why, but for some reason I kept thinking Rectangle inherited the methods from Point.

    Woot, thanks, I got it, and if anyone is curious, I just changed the translate method so it doesn't return anything and to this

    -(void) translate: (Point *) vector;
    	float vectorX, vectorY;
    	Point* transOrigin = [[Point alloc] init];
    	vectorX = [vector x] + [[self origin] x];
    	vectorY = [vector y] + [[self origin] y];
    	[transOrigin setX: vectorX setY: vectorY];
    	[self setOrigin: transOrigin];

Share This Page