Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
August 22, 2017, 07:38:38 PM *
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: Program18-2 from Programming in C returns warnings when compiled  (Read 3559 times)
Alla
Newbie
*
Posts: 1


Email
« on: November 10, 2015, 04:28:29 AM »

Hello!

I am new to this forum, and new to programming, although I have already reached chapter 18
of Programming in C textbook. If I understand correctly, this forum is dedicated to Objective-C
only, and Objective-C is different from C language.
Please, take a look at the program I typed in from the book, and warning messages I receive.
I would be grateful for explanation on what I am doing wrong. I work on Mac OS 10.7.5, Xcode 4.6.3,
Terminal 2.2.3.
1) I created a file with the command:
touch prog18-2.m// Program ot work with fractions - Objective-C version

2) Program
#import <stdio.h>
#import <objc/Object.h>

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

@interface Fraction: Object
{
    int numerator;
    int denominator;
}
-(void) set_numerator: (int) n;
-(void) set_denominator: (int) d;
-(void) print;

@end

// ----- @implementation section -------

@implementation Fraction;

// getters
-(int) numerator
{
    return numerator;
}

-(int) denominator
{
    return denominator;
}

//setters
-(void) set_numerator: (int) num
{
    numerator = num;
}

-(void) set_denominator: (int) denom
{
    denominator = denom;
}

//other
-(void) print
{
    printf("The value of the fraction is %i/%i\n", numerator, denominator);
}

@end

//-------- program section -----------

int main(void)
{
    Fraction* my_fract;
    my_fract = [Fraction new];
   
    [my_fract set_numerator: 1];
    [my_fract set_denominator: 3];
   
    printf("The numerator is %i, and teh denominator is %i\n", [my_fract numerator], [my_fract denominator]);
    [my_fract print];
   
    [my_fract free]; //frees the memory that was used by Fraction object
   
    return 0;
}

3) I compiled it with:
gcc -framework Foundation prog18-2.m -o prog18-2

4) Terminal generated the following:
prog18-2.m: In function ‘main’:
prog18-2.m:58: warning: ‘Fraction’ may not respond to ‘+new’
prog18-2.m:58: warning: (Messages without a matching method signature
prog18-2.m:58: warning: will be assumed to return ‘id’ and accept
prog18-2.m:58: warning: ‘...’ as arguments.)
prog18-2.m:71: warning: ‘Fraction’ may not respond to ‘-free’

Thank you!

Logged
mitchb
Full Member
***
Posts: 154


« Reply #1 on: November 12, 2015, 04:09:28 PM »

Most of this forum is for objective-C but this little corner is for Steve's book on C.

My take on a c version would be:

Code: (Objective-C)
#include <stdio.h>

struct fraction
{
    int numerator;
    int denominator;
};


int main(int argc, const char * argv[])
{
    struct fraction my_Fraction = {1, 3};
   
    printf("%i/%i\n", my_Fraction.numerator, my_Fraction.denominator);
   
    return 0;
}

I chose a struct so an array of structs could be used to hold many fractions if needed.

An objective-C program to do the same thing could look something like this:

Code: (Objective-C)
#import <Foundation/Foundation.h>

@interface Fraction : NSObject

- (void)print;
- (void)setNumerator:(int)n;
- (void)setDenominator:(int)d;
- (int)numerator;
- (int)denominator;
- (double)convertToNum;

@end

@implementation Fraction
{
    int numerator;
    int denominator;
}

- (void)print
{
    int remainder;
   
    if ( numerator == 0 )
        NSLog(@" 0");
   
    else if ( denominator == 1 )
        NSLog(@" %i", numerator);
   
    else if ( numerator > denominator )
    {
        remainder = numerator % denominator;
       
        if ( remainder == 0 )
            NSLog(@" %i", numerator / denominator);
       
        else
            NSLog(@" %i %i/%i", numerator / denominator, remainder, denominator);
    }
    else
        NSLog(@" %i/%i", numerator, denominator);
}

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

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

- (int)numerator
{
    return numerator;
}

- (int)denominator
{
    return denominator;
}

- (double)convertToNum
{
    if ( denominator != 0 )
        return (double) numerator / denominator;
    else
        return NAN;
}

@end


#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

    @autoreleasepool
    {
        Fraction *myFraction = [[Fraction alloc] init];
       
            // set fraction to 1/3
        [myFraction setNumerator:1];
        [myFraction setDenominator:3];
       
            // display the fraction.
        NSLog(@"The value of myFraction is:");
        [myFraction print];
    }
    return 0;
}

Since you are still on Lion, the arc portion (@autoreleasepool) won't compile on your system. Just an example of objective-C code.
Logged

If you give a man a program, you will frustrate him for a day;
If you teach him how to program, you will frustrate him for a lifetime;
     - Anonymous
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.