Amazon.com Widgets Prog 3.2 errors :(
Welcome, Guest. Please login or register.
Did you miss your activation email?
June 19, 2013, 02:44:09 AM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  Programming in Objective-C, 4th edition
| |-+  Chapter 3
| | |-+  Prog 3.2 errors :(
Pages: [1] 2   Go Down
Print
Author Topic: Prog 3.2 errors :(  (Read 2048 times)
a13x612
Newbie
*
Posts: 10


Email




« on: February 03, 2012, 11:55:29 PM »

Hi all,

First time on boards, just started learning objective-c.  I'm still pretty new to programming and have limited experience so please bear with me.

I typed prog 3.2 into xcode but i keep getting errors.  Below is the code and attached is the screenshot of the errors:

#import <Foundation/Foundation.h>

//---- @interface section
@interface Fraction: NSObject

-(void) print;
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;

@end

//-----implementation section
@implementation Fraction
{
    int numerator;
    int denominator;
}

-(void) print
{
    nslog(@"%i/%i",numerator,denominator);
}

-(void) setnumerator: (int) n
{
    numerator = n;
}

-(void) setdenominator: (int) d
{
    denominator = d;
}

@end;

//----program section

int main (int argc, char * argv[])
{
   
    @autoreleasepool {
        Fraction *myFraction;
       
        //create an instance of a fraction.
       
        myFraction=[Fraction alloc];
        myFraction=[myFraction init];
       
        //set fraction to 1/3
       
        [myFraction setNumerator:1];
        [myFraction setDenominator:3];
       
        //Display fraction using print method
       
        NSLog(@"The value of myFraction is:");
        [myFraction print];
       
    }
    return 0;
}

Can anyone help me out on this?

Thanks!
Logged
skochan
Administrator
Hero Member
*****
Posts: 3109







« Reply #1 on: February 04, 2012, 12:38:03 AM »

Pay attention to case in your letters; case is significant.

setNumerator
setDenominator
NSLog

I don't understand the "Inconsistent number of instance variables error."  I copied your posted code into Xcode and I'm not seeing that error.  I'll try to look further.

Cheers,

Steve
Logged
a13x612
Newbie
*
Posts: 10


Email




« Reply #2 on: February 04, 2012, 09:19:37 AM »

I fixed the syntax on everything and i retyped the lines with the strange errors on them, ran it, and it worked! don't know what i did exactly to make them go away. thank you for your help, i will definitely be back!
Logged
skochan
Administrator
Hero Member
*****
Posts: 3109







« Reply #3 on: February 04, 2012, 11:57:19 AM »

Yoiu're welcome!  We're always here to help.!  Smiley

Cheers,

Steve
Logged
a13x612
Newbie
*
Posts: 10


Email




« Reply #4 on: February 04, 2012, 01:13:15 PM »

In an attempt to understand what happened to make it work I retyped the code and the same errors are popping up again.


#import <Foundation/Foundation.h>

//--- @interface section ---

@interface Fraction: NSObject

-(void) print;
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;

@end

//--- @implementation section ---

@implementation Fraction

{
    int numerator;
    int denominator;
}
-(void) print
{
    NSLog(@"%i/%i", numerator, denominator);
}

-(void) setNumerator: (int) n
{
    numerator = n;
}

-(void) setDenominator: (int) d
{
    denominator = d;
}

@end

//--- program section ---

int main (int argc, char * argv[])
{
    @autoreleasepool {
        Fraction *myFraction;
       
        //Create an instance of a Fraction
       
        myFraction = [Fraction alloc];
        myFraction = [myFraction init];
       
        //Set fraction to 1/3
       
        [myFraction setNumerator: 1];
        [myFraction setDenominator: 3];
       
        //Display fraction using print method
       
        NSLog (@"The value of myFraction is:");
        [myFraction print];
    }
   
    return 0;
}

See attached image for errors.  This is getting beyond frustrating and i'm thinking it is the actual software that is screwing up and not the code itself.
Logged
Waiting
Global Moderator
Full Member
*****
Posts: 103






« Reply #5 on: February 04, 2012, 01:55:34 PM »

I copy and pasted this into a fresh file and it works fine.

Maybe try a fresh project.


In an attempt to understand what happened to make it work I retyped the code and the same errors are popping up again.


#import <Foundation/Foundation.h>

//--- @interface section ---

@interface Fraction: NSObject

-(void) print;
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;

@end

//--- @implementation section ---

@implementation Fraction

{
    int numerator;
    int denominator;
}
-(void) print
{
    NSLog(@"%i/%i", numerator, denominator);
}

-(void) setNumerator: (int) n
{
    numerator = n;
}

-(void) setDenominator: (int) d
{
    denominator = d;
}

@end

//--- program section ---

int main (int argc, char * argv[])
{
    @autoreleasepool {
        Fraction *myFraction;
       
        //Create an instance of a Fraction
       
        myFraction = [Fraction alloc];
        myFraction = [myFraction init];
       
        //Set fraction to 1/3
       
        [myFraction setNumerator: 1];
        [myFraction setDenominator: 3];
       
        //Display fraction using print method
       
        NSLog (@"The value of myFraction is:");
        [myFraction print];
    }
   
    return 0;
}

See attached image for errors.  This is getting beyond frustrating and i'm thinking it is the actual software that is screwing up and not the code itself.

Logged
a13x612
Newbie
*
Posts: 10


Email




« Reply #6 on: February 04, 2012, 02:51:07 PM »

Tried that.  is it because i'm running xcode 4 on snow leopard?
Logged
skochan
Administrator
Hero Member
*****
Posts: 3109







« Reply #7 on: February 04, 2012, 03:50:21 PM »

It's bizarre.

Steve
Logged
a13x612
Newbie
*
Posts: 10


Email




« Reply #8 on: February 04, 2012, 04:01:35 PM »

I figured it out.

In the implementation section:

//--- @implementation section ---

@implementation Fraction

{
    int numerator;
    int denominator;
}
-(void) print
{
    NSLog(@"%i/%i", numerator, denominator);
}

-(void) setNumerator: (int) n
{
    numerator = n;
}

-(void) setDenominator: (int) d
{
    denominator = d;
}

@end

The issue was the curly braces before and after "int numerator" and "int denominator".  Once I removed those, the program worked.

-.-
Logged
Deusexmachina
Newbie
*
Posts: 1






« Reply #9 on: February 04, 2012, 04:29:43 PM »

Hi,

It might be a version thing. I've tried to follow some XCode 4 examples and got several errors because I had a newer version (xcode 4.2xxx). I downloaded my version of Xcode last November but I've seen diffences with code written back in July.

What version of Xcode do you see in the splash screen when you start the program?

Best,

DEM
Logged
a13x612
Newbie
*
Posts: 10


Email




« Reply #10 on: February 06, 2012, 03:47:01 PM »

Its version 4.2.  But then my question becomes how can I keep up if the code is always changing? And where can I go to look at updated versions of these examples (if possible)?
Logged
Bunchadna
Newbie
*
Posts: 27






« Reply #11 on: March 20, 2012, 11:44:15 PM »

I got the same errors

use of undeclared identifier 'n' 

If I expand the message, it says

local declaration of 'numerator' hides instance variable

Removing the braces didn't help.

I'll post a screen shot later - when I figure out how to do it.  I'm brand new to mac and am pedaling quickly up the learning curve ... ;-)


Logged
Bunchadna
Newbie
*
Posts: 27






« Reply #12 on: March 29, 2012, 09:53:18 PM »

I'm having trouble and really need help.  I was getting the same error messages, so I tried changing the name of the variable to "mynumerator" and "mydenominator" because the error message seemed to indicate that the names "numerator" and "denominator" were reserved in some sense.  Anyway, here's my code.  Any help is greatly appreciated.  Note that I still get errors "Local declaration of "mynumerator" hides instance variable."


#import <Foundation/Foundation.h>

// ----------- @interface section ---------

@interface Fraction: NSObject

-(void)     print;
-(void)     setNumerator:  (int) n;
-(void)     setDenominator:  (int) d;

@end

//------@Implementation section ----

@implementation Fraction

{    
    int mynumerator;
    int mydenominator;
}

-(void)  print
{
    NSLog (@"\n%i/%i", mynumerator, mydenominator);
}

-(void) setNumerator:  (int) mynumerator
{
    mynumerator = n;
}

-(void) setDenominator:  (int) mydenominator
{
    mydenominator = d;
}

@end

//------Program section ------

int main (int argc, char * argv[])
{
    @autoreleasepool {
        Fraction  *myFraction;
        
        //  Create an instance of a fraction
        
        myFraction = [Fraction alloc];
        
        myFraction = [myFraction init];
        
        //  Set fraction to 1/3
        
        [myFraction setNumerator: 1];
        [myFraction setDenominator: 3];
        
        //  Display the fraction using print method
        
        NSLog (@"\nThe value of myFraction is:");
        [myFraction print];
        
    }
    
    return 0;
}
« Last Edit: March 29, 2012, 10:49:55 PM by Bunchadna » Logged
Waiting
Global Moderator
Full Member
*****
Posts: 103






« Reply #13 on: March 30, 2012, 11:31:06 AM »

Code: (Objective-C)
@interface Fraction: NSObject

-(void)     setNumerator:  (int) n;
-(void)     setDenominator:  (int) d;

@end

-(void) setNumerator:  (int) mynumerator
{
    mynumerator = n;
}

-(void) setDenominator:  (int) mydenominator
{
    mydenominator = d;
}

@end

In the interface section you say you will send setNumerator an integer called n, then in the implementation section you say you will send it the int mynumerator.

The compiler is confused by the use of mynumerator when it is already in the fraction implementation.

Secondly you try to set mynumerator to n, however n does not exist as you have changed the implementation so that it is no longer there.

Look at the code below and it should help you realise where you went wrong.

Code: (Objective-C)
//--- @interface section ---

@interface Fraction: NSObject

-(void) print;
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;

@end

//--- @implementation section ---

@implementation Fraction

{
    int numerator;
    int denominator;
}
-(void) print
{
    NSLog(@"%i/%i", numerator, denominator);
}

-(void) setNumerator: (int) n
{
    numerator = n;
}

-(void) setDenominator: (int) d
{
    denominator = d;
}


Logged
Bunchadna
Newbie
*
Posts: 27






« Reply #14 on: March 30, 2012, 06:24:16 PM »

Thank you for responding.  I got it to work the way it's presented in the text.
Logged
Pages: [1] 2   Go Up
Print
Jump to:  



Login with username, password and session length

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 (c) 2009 classroomM.com. All rights reserved.