Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
April 22, 2018, 09:07:17 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register Chat  
Pages: [1]   Go Down
  Print  
Author Topic: Undeclared Variable: Everything seems fine though??  (Read 3471 times)
garymcnamara
Newbie
*
Posts: 3


Email
« on: September 10, 2010, 04:14:05 PM »

Hi everyone, would appreciate some help to see the errors of my ways.
Xcode keeps throwing back:
'xCoordinate' undeclared, first use in this function
'yCoordinate undeclared, first use in this function

here is my code:
#import <Foundation/Foundation.h>

/*define a class that will hold a cartesian co-ordinate, (x,y), where x and y are integers.
 Define methods to individually set the x and yco-ordinates of a point and retrieve their values.*/


@interface XYpoint: NSObject
{
   int xCoordinate;
   int yCoordinate;
}

-(void) print;
-(int)  xCoordinate;
-(int)  yCoordinate;
-(void) setXCoordinate: (int) x;
-(void) setYCoordinate: (int) y;

@end

@implementation XYpoint

-(void) setXCoordinate: (int) x
{
   xCoordinate = x;
}

-(void) setYCoordinate: (int) y
{
   yCoordinate = y;
}

-(int) xCoordinate
{
   return xCoordinate;
}

-(int) yCoordinate
{   
   return yCoordinate;
}


@end


int main (int argc, char * argv[])
{
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   XYpoint *myXYpoint;
   
   [myXYpoint setXCoordinate: 12];
   [myXYpoint setYCoordinate: 24];
   
   NSlog (@"The Co-Ordinates are %i,%i", xCoordinate, yCoordinate);

   [myXYpoint release];
   [pool drain];
   return 0;
}
Logged
pdxnative
Newbie
*
Posts: 11


« Reply #1 on: September 10, 2010, 04:58:48 PM »

Hi Gary - Welcome!

Well....You have a few issues going on.

1. You did not actually create (instantiate) your myXYPoint object. 'XYpoint *myXYpoint' just creates a reference but you still have to initialize and allocate memory (those are hints BTW).

2. When ever you call a method you need to put it in [brackets] and make sure you use the object reference before the method ( myXYpoint ).

3. after those items you will see that you have a typo with NSLog. Wink

Your program will now run successfully but you actually still have one more minor issue that the compiler ignores...Compare your interface section with your implementation. Wink

Hope this helps without giving too much away. Keep posting!
Logged
garymcnamara
Newbie
*
Posts: 3


Email
« Reply #2 on: September 11, 2010, 01:17:51 AM »

wow thank you very much. I needed an explanation of where I was going wrong than someone giving me the answers, so great reply!
I will re read the chapter as I clearly missed some of the key points, and give it another shot.
Thanks for your help, will update you on my success!!
Logged
garymcnamara
Newbie
*
Posts: 3


Email
« Reply #3 on: September 11, 2010, 03:37:23 AM »

OK it works!!

Here is the refined version:

#import <Foundation/Foundation.h>

/*define a class that will hold a cartesian co-ordinate, (x,y), where x and y are integers.
 Define methods to individually set the x and yco-ordinates of a point and retrieve their values.*/


@interface XYpoint: NSObject
{
   int xCoordinate;
   int yCoordinate;
}

-(void) print;
-(void) setXCoordinate: (int) x;
-(void) setYCoordinate: (int) y;

@end

@implementation XYpoint
-(void) print
{
   NSLog(@"%i,%i", xCoordinate, yCoordinate);
}

-(void) setXCoordinate: (int) x
{
   xCoordinate = x;
}

-(void) setYCoordinate: (int) y
{
   yCoordinate = y;
}
@end


int main (int argc, char * argv[])
{
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   XYpoint *myXYpoint;
   
   myXYpoint = [[XYpoint alloc]init];
   
   [myXYpoint setXCoordinate: 12];
   [myXYpoint setYCoordinate: 24];
   
   NSLog (@"The Co-Ordinates are:");
   [myXYpoint print];

   [myXYpoint release];
   [pool drain];
   return 0;
}

To be honest, I understood your hint on the init and alloc on memory (although I was trying to do both on seperate lines, and xcode kept throwing an exception. I went form

myXYpoint [alloc];
myXYpoint [init];

to this:

myXYpoint [[alloc] init];

Not fully understand why the latter workd and the first did not though...

Did not understand point 2. In the end i removed

-(int)  xCoordinate;
-(int)  yCoordinate;

from the method declarations as I already introduced the instance variables in the member declarations section.  As they were removed from the interface, I also removed them from implementation.

Good catch on the typo of NSLog ;-)

As for the last point, my compiler threw no minor issues, did i rectify them by removing the not needed method declarations?

Thanks for all your help!
Gary
Logged
pdxnative
Newbie
*
Posts: 11


« Reply #4 on: September 11, 2010, 06:55:41 AM »


To be honest, I understood your hint on the init and alloc on memory (although I was trying to do both on seperate lines, and xcode kept throwing an exception. I went form

myXYpoint [alloc];
myXYpoint [init];

to this:

myXYpoint [[alloc] init];

Not fully understand why the latter workd and the first did not though...

myXYpoint = [[XYpoint alloc]init]; <<--- This nested structure is required for context to the compiler. In other words it is saying initialize the returned result of the XYpoint memory allocation.


myXYpoint [alloc];  <<--- may return and address (ex: 0AFFFF)
myXYpoint [init];  <<--- may return an error. "init what?"

Quote


Did not understand point 2. In the end i removed

-(int)  xCoordinate;
-(int)  yCoordinate;

from the method declarations as I already introduced the instance variables in the member declarations section.  As they were removed from the interface, I also removed them from implementation.

In your original post you had a print method in your interface but did not have a print implementation.

Quote
Good catch on the typo of NSLog ;-)

As for the last point, my compiler threw no minor issues, did i rectify them by removing the not needed method declarations?

Thanks for all your help!
Gary

Happy to help out.....Helps solidify what I am learning too!
« Last Edit: September 12, 2010, 09:40:12 AM by pdxnative » Logged
shawnpk
Newbie
*
Posts: 10


« Reply #5 on: December 10, 2010, 02:41:32 PM »

Also, when you call alloc, the receiver of the message should be the class itself not the instance of the class.  So it should read:

[XYPoint alloc], not [myXYPoint alloc].

If you combine these calls onto one line the it would read:

XYPoint *myXYPoint = [XYPoint alloc] init];

This line both allocates your instance and initializes it.  This is much easier to read and is the standard convention in Objective-C.
« Last Edit: December 10, 2010, 03:32:00 PM by shawnpk » Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents ゥ 2009 classroomM.com. All rights reserved.