CaptainZap

Feb 10, 2007, 04:27 PM

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,

4. Write a Rectangle method called translate: that takes a vector Point (xv,yv) as its argument. Have it translate the rectangle's origin by the specified vector.

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.

4. Write a Rectangle method called translate: that takes a vector Point (xv,yv) as its argument. Have it translate the rectangle's origin by the specified vector.

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.