Weird problem passing integer variables..

Apprentice
Posts: 5
Joined: 2009.03
Post: #1
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:

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]
Gives me values like 1086324736 for xPos and yPos.. Blink

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 Annoyed

Any hints ?


I am using Mac OS 10.5.6 XCode version 3.1.2
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #2
Are xPos and yPos declared inside your @interface { ... } block? I can't tell from the snippet you pasted.
Quote this message in a reply
Apprentice
Posts: 5
Joined: 2009.03
Post: #3
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 Sad
Quote this message in a reply
Moderator
Posts: 608
Joined: 2002.04
Post: #4
Clean all + rebuild?
Quote this message in a reply
Apprentice
Posts: 5
Joined: 2009.03
Post: #5
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 Wacko After that I renamed the instance method from
Code:
-(id)initWithX: (int)xtmp Y:(int)ytmp

to
Code:
-(id)initWithXPos: (int)xtmp YPos:(int)ytmp

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...
Quote this message in a reply
DoG
Moderator
Posts: 869
Joined: 2003.01
Post: #6
there's probably an
Code:
-(id)initWithX: (double)x Y:(double)x;
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.
Quote this message in a reply
Apprentice
Posts: 5
Joined: 2009.03
Post: #7
Hey DoG,

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.. Ninja

Thanks for your help !
Quote this message in a reply
Post Reply 

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