PDA

View Full Version : Help with values not being changed




wiseone9
Aug 21, 2010, 04:58 PM
hey there,
im very new to objective c programming, and i'm having trouble getting the values contained in a class to change. ill just go straight to the problem

so basically i created a class called vector that contains variables like this:

Vector.h:

@interface Vector : NSObject {
float X;
float Y;
float magnitude;
float direction;
}
@property float X;
@property float Y;
@property float magnitude;
@property float direction;


- (Vector *)setVector:(float *)newX : (float *)newY;

Vector.m:
#import "Vector.h"


@implementation Vector

@synthesize X;
@synthesize Y;
@synthesize magnitude;
@synthesize direction; //goes on for a while this is just the beginning

now, i have this function that is supposed to set a vector inside Vector.m. this is what it looks like:

- (Vector *) setVector:(float *)newX : (float *)newY{
Vector *v1;
v1.X = *newX;
v1.Y = *newY;
v1.magnitude = sqrtf(powf(X,2) + powf(Y, 2));
v1.direction = atanf(Y/X);
return v1;
}

but when i say

vector1 = [vector1 setVector: &xfloat : &yfloat];
(xfloat and yfloat do have a value, i have confirmed this)

when i output vector1.X and vector1.Y i get 0 and 0...whats going on?
let me know if this is not clear



robbieduncan
Aug 21, 2010, 05:02 PM
- (Vector *) setVector:(float *)newX : (float *)newY{
Vector *v1;
v1.X = *newX;
v1.Y = *newY;
v1.magnitude = sqrtf(powf(X,2) + powf(Y, 2));
v1.direction = atanf(Y/X);
return v1;
}


At no point do you set v1 to anything: all you have done is declare a pointer to an object of type v1. That just reserves storage space for the pointer. It does not create a new object.

Also that method is very odd. Why does your set method (which would normally set the values of the existing method) return a new vector instead of setting the values in the instance the method is called on?

wiseone9
Aug 21, 2010, 07:03 PM
At no point do you set v1 to anything: all you have done is declare a pointer to an object of type v1. That just reserves storage space for the pointer. It does not create a new object.

Also that method is very odd. Why does your set method (which would normally set the values of the existing method) return a new vector instead of setting the values in the instance the method is called on?

Ok so if i read your post right, this is what you meant:

- (Vector *) setVector:(float *)newX : (float *)newY{

X = *newX;
Y = *newY;
magnitude = sqrtf(powf(X,2) + powf(Y, 2));
direction = atanf(Y/X);
return self;
}

but it still does not work... i tried doing both:
vector1 = [vector1 setVector: &xfloat : &yfloat];
and just
[vector1 setVector: &xfloat : &yfloat];

chown33
Aug 21, 2010, 08:02 PM
How did you determine it didn't work? Debugger? NSLog? printf?

Post the code that shows whatever you did to determine it didn't work.

Debugging is mostly a process of gathering useful evidence. We need to see the evidence (i.e. the output) as well as the evidence-gatherer.

wiseone9
Aug 21, 2010, 08:19 PM
basically i just output it to a label that says "X coordinate: " and then a float value

when i output vector1.X, it always outputs 0, but if i output xfloat, which is what i tried to set vector1.X equal to, it outputs the proper number. (which is the number i enter in a text field above the label). therefore, i know that xfloat is being set to the correct number, but vector1.X just stays at 0

dejo
Aug 21, 2010, 08:29 PM
- (Vector *) setVector:(float *)newX : (float *)newY{

X = *newX;
Y = *newY;
...

float is a primitive. What is the need for all the screwy pointers?

Plus, setters normally just return void.

P.S. Have you instantiated your vector1 yet?

chown33
Aug 21, 2010, 08:53 PM
basically i just output it to a label that says "X coordinate: " and then a float value

when i output vector1.X, it always outputs 0, but if i output xfloat, which is what i tried to set vector1.X equal to, it outputs the proper number. (which is the number i enter in a text field above the label). therefore, i know that xfloat is being set to the correct number, but vector1.X just stays at 0

Post your code.

Describing code does nothing. We can't debug descriptions. Even if we could, the translation from description to code could be incorrect.

EDIT:
Test program and output is below.
Compiled as command-line tool for Mac OS 10.6.3.

Output:
2010-08-21 19:12:33.975 a.out[1847:903] X:0.000000 Y:0.000000 mag:0.000000 dirn:0.000000
2010-08-21 19:12:33.979 a.out[1847:903] X:12.300000 Y:12.400000 mag:17.465681 dirn:0.789447


vecmain.m:
#import <Foundation/Foundation.h>

#import "Vector.h"

static void
showVec( Vector * vec )
{
NSLog( @" X:%f Y:%f mag:%f dirn:%f", vec.X, vec.Y, vec.magnitude, vec.direction );
}


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

Vector * vec1 = [[Vector alloc] init]; // initially empty

showVec( vec1 );

float x1 = 12.3f;
float y1 = 12.4f;

[vec1 setVector:&x1 :&y1];
showVec( vec1 );

[pool drain];
return 0;
}

Vector.h:
#import <Foundation/Foundation.h>

@interface Vector : NSObject {
float X;
float Y;
float magnitude;
float direction;
}
@property float X;
@property float Y;
@property float magnitude;
@property float direction;


- (Vector *)setVector:(float *)newX : (float *)newY;

@end


Vector.m:
#import "Vector.h"

@implementation Vector

@synthesize X;
@synthesize Y;
@synthesize magnitude;
@synthesize direction; //goes on for a while this is just the beginning

- (Vector *) setVector:(float *)newX : (float *)newY{
X = *newX;
Y = *newY;
magnitude = sqrtf(powf(X,2) + powf(Y, 2));
direction = atanf(Y/X);
return self;
}

@end

wiseone9
Aug 21, 2010, 09:30 PM
wow thanks for all the help everybody. it turns out that all my code is fine, i had just forgotten to instantiate vector1. (the ironic thing is that i had instantiated my vector2, but i never tested that one)

anyways, thanks and sorry for making such a big deal about a really dumb mistake