Weird problem passing integer variables..
Hey guys,
I am having a weird problem with an Objective-C class I wrote. The problem itself is explained pretty quickly.
In my class GridPosition I have (among other things) two variables and a constructor:
The problem is that whatever arguments I pass to the instance method, the values xPos and yPos always seemed to be randomly assigned !
For example:
Gives me values like 1086324736 for xPos and yPos.. 
Debugging shows that the instance method is indeed called with these values above. But how is this possible ? I passed in 6 and 6 ! I remember stumbling over this very problem a few month ago, but it somehow resolved itself - unfortunately that's not the case this time
Any hints ?
I am using Mac OS 10.5.6 XCode version 3.1.2
I am having a weird problem with an Objective-C class I wrote. The problem itself is explained pretty quickly.
In my class GridPosition I have (among other things) two variables and a constructor:
Code:
//in GridPosition.h
int xPos;
int yPos;
//in GridPosition.m
-(id)initWithX: (int)xtmp Y:(int)ytmp{
if (self = [super init]){
xPos = xtmp;
yPos = ytmp;
}
return self;
}The problem is that whatever arguments I pass to the instance method, the values xPos and yPos always seemed to be randomly assigned !
For example:
Code:
[[GriPosition alloc] initWithX:6 Y:6]
Debugging shows that the instance method is indeed called with these values above. But how is this possible ? I passed in 6 and 6 ! I remember stumbling over this very problem a few month ago, but it somehow resolved itself - unfortunately that's not the case this time

Any hints ?
I am using Mac OS 10.5.6 XCode version 3.1.2
Are xPos and yPos declared inside your @interface { ... } block? I can't tell from the snippet you pasted.
ThemsAllTook Wrote:Are xPos and yPos declared inside your @interface { ... } block? I can't tell from the snippet you pasted.
Yup, the declaration is inside an interface block. I looked at the syntax over and over but there seems to be nothing wrong with it
Clean all + rebuild?
Ok, I cleaned the project and built it again, but no luck. Next I removed the GridPosition class from my project (deleting the header and source files) and started to re-implement the class: The problem stayed
After that I renamed the instance method from
to
And things seem to work properly now. But this is so strange, I wish I knew what the cause for this problem was (or is).
Well thanks for your suggestions so far, anyway...
After that I renamed the instance method from Code:
-(id)initWithX: (int)xtmp Y:(int)ytmpto
Code:
-(id)initWithXPos: (int)xtmp YPos:(int)ytmpAnd things seem to work properly now. But this is so strange, I wish I knew what the cause for this problem was (or is).
Well thanks for your suggestions so far, anyway...
there's probably an
method declared somewhere else, and since -alloc returns id, the compiler choses whichever comes first in the module. If you included the right header files, you should at least have seen a compiler warning.
Code:
-(id)initWithX: (double)x Y:(double)x;
Hey DoG,
well I have a Vector class which has a similar method signature
But I don't remember getting a compiler warning (if so I would have tracked down the problem, I'm sure)
But yeah, something must be getting mixed up during compilation.. I just can't pinpoint it. Next time the problem shows up, I will investigate further. For now I will just shrug it off and continue development..
Thanks for your help !
well I have a Vector class which has a similar method signature
Code:
-(id)initWithX:(int)x Y:(int)y Z:(int)z;But I don't remember getting a compiler warning (if so I would have tracked down the problem, I'm sure)
But yeah, something must be getting mixed up during compilation.. I just can't pinpoint it. Next time the problem shows up, I will investigate further. For now I will just shrug it off and continue development..

Thanks for your help !
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Accelerate and working with Integer Arrays SIMD | Bersaelor | 6 | 5,036 |
Jul 6, 2010 07:56 AM Last Post: Bersaelor |
|
| exporting c functions and variables | NelsonMandella | 6 | 4,264 |
Apr 1, 2010 05:07 PM Last Post: NelsonMandella |
|
| Getting a function to recognize an integer | FlamingHairball | 7 | 4,301 |
Jan 20, 2008 06:35 AM Last Post: FlamingHairball |
|
| Adding Member Variables to Classes | FlamingHairball | 2 | 2,775 |
Jan 12, 2008 02:43 PM Last Post: FlamingHairball |
|
| reading integer input from user in C | anthony | 4 | 5,750 |
Nov 24, 2007 02:38 PM Last Post: unknown |
|

