Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Feb 10, 2007, 04:27 PM   #1
CaptainZap
macrumors regular
 
Join Date: Jan 2007
Probably a Simple Problem...

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,
Quote:
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,
Code:
-(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...
Code:
-(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

Code:
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.
CaptainZap is offline   0 Reply With Quote
Old Feb 10, 2007, 04:35 PM   #2
bronxbomber92
macrumors regular
 
Join Date: Nov 2006
It would help to know what the structs/classes of Point and Rect contained
bronxbomber92 is offline   0 Reply With Quote
Old Feb 10, 2007, 04:38 PM   #3
CaptainZap
Thread Starter
macrumors regular
 
Join Date: Jan 2007
K, for Rectangle...
Code:
@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;

@end
And for Point
Code:
@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;

@end
P.S. I left out the preprocessor lines, so don't think that I forgot to put them in my program :P
CaptainZap is offline   0 Reply With Quote
Old Feb 10, 2007, 05:42 PM   #4
yoavcs
macrumors regular
 
Join Date: Apr 2004
Location: Israel
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.

Last edited by yoavcs; Feb 10, 2007 at 06:00 PM.
yoavcs is offline   0 Reply With Quote
Old Feb 10, 2007, 06:08 PM   #5
CaptainZap
Thread Starter
macrumors regular
 
Join Date: Jan 2007
Quote:
Originally Posted by yoavcs View Post
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 [ ] method call, the ones outside.
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

Code:
-(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];
	
}

Last edited by CaptainZap; Feb 10, 2007 at 06:16 PM.
CaptainZap is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Serious problem - simple and funny solution.. Thrash911 iMac 4 Oct 26, 2013 07:30 PM
A serious problem, with a simple solution? NaughtyNoNo Mac Basics and Help 4 Aug 8, 2013 12:37 PM
Problem reading from input file in simple console application Tommas Mac Programming 8 Aug 5, 2013 04:49 PM
Simple problem, need help! Sweetooth44 iPhone/iPad Programming 5 Sep 19, 2012 11:35 AM
a simple fix to annoying little problem. steve knight iPhone Tips, Help and Troubleshooting 0 Aug 3, 2012 11:42 AM

Forum Jump

All times are GMT -5. The time now is 04:05 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC