Amazon.com Widgets while this codes refuse to build success?
Welcome, Guest. Please login or register.
Did you miss your activation email?
June 19, 2013, 12:56:36 PM
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
| | |-+  while this codes refuse to build success?
Pages: [1]   Go Down
Print
Author Topic: while this codes refuse to build success?  (Read 609 times)
XT
Newbie
*
Posts: 1






« on: November 21, 2012, 10:18:35 AM »

Please can someone assist me see why this codes refuse to build success.
I am with current Xcode 4.....
Thx




//program to work with fractions --class version


#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 = [Fraction init];
               
                // set Fraction to 1/3
               
                [myFraction setNumerator:1];
                [myFraction setDenominator:3];
            //Display the fraction using the print method
               
                NSLog (@"The value of myFraction is:");
                [myFraction print];
           
           
           
        }
            return 0;
        }








Logged
afterDark
Global Moderator
Full Member
*****
Posts: 126






« Reply #1 on: November 21, 2012, 11:13:21 AM »

hi XT!

I have just started in the book myself (now in chapter 9) so I am not an experienced programmer, but I will try to help.

Actually your code builds fine, when I put it in XCode. It does not run though.

This is from your code, in the 'main' part:

 
Code: (Objective-C)
Fraction * myFraction;
  //create an instance of a Fraction
 
 myFraction = [Fraction alloc];
 myFraction = [Fraction init];

In line 4 you call the Fraction class to allocate memory for a new instance, and return that instance to myFraction. Then in line 5 you call the Fraction class again, this time to initialize. But it is not the class that should initialize itself, it is the newly allocated instance that should initialize itself.

This works better:

Code: (Objective-C)
Fraction * myFraction;
  //create an instance of a Fraction
 
myFraction = [Fraction alloc];
myFraction = [myFraction init];

There in line 5 the newly allocated instance myFraction receives the message to initialize. The result of that, the initialized instance, is then the new value of myFraction.

The following has the exact same result, but might help better in understanding what is going on:

Code: (Objective-C)
Fraction * myFraction;
Fraction * myIntermediateFraction;
  //create an instance of a Fraction
 
myIntermediateFraction = [Fraction alloc];
myFraction = [myIntermediateFraction init];

Logged

I am just an amateur with Objective-C, don't let the moderator label fool you. Working my way through the book slowly.
aleksbreian1103
Newbie
*
Posts: 22






« Reply #2 on: December 21, 2012, 10:51:42 PM »

or just write
 
Code: (Objective-C)
myFraction = [[Fraction alloc] init];  
to save time and space.
The answer above is correct though. You allocate memory to a pointer to the Fraction class, then you initialize your new instance.
Logged
Pages: [1]   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.