maverick
maverick

Reputation: 193

objective C new method not defined may be the issue

I have the following code in objective-c, with an warning. The posts I reviewed about it seem to imply that it is not a huge issue; it is a method that is automatically created. In my case though, does not give the result if should. I am not sure what I am doing wrong. Given that this is one of the first few programs in objective-C I am doing, I like to validate it so I can become familiar with the right syntax. Here is the code:

#import <stdio.h>
#import <objc/Object.h>
//---------------interface section-------------

@interface Fraction: Object
{
  int numerator;
  int denominator;
}
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;
-(void) print;
@end
//-----------------implementation section-------
@implementation Fraction;
//getters
-(int) numerator
{
  return numerator;
}
-(int) denominator
{
  return denominator;
}
//setters
-(void) setNumerator: (int) num
{
  numerator = num;
}
-(void) setDenominator: (int) denom
{
  denominator = denom;
}
-(void) print
{
  printf("The value of the fraction is %i/%i\n", numerator, denominator);
}
@end
//-----------------program section-------------------------
int main (void)
{
  Fraction *myFract;
  myFract = [Fraction new];
  [myFract setNumerator: 1];
  [myFract setDenominator: 3];

  printf ("The numerator is %i, and the denominator is %i\n", [myFract numerator], [myFract denominator]);
  [myFract print];
  [myFract free];
  return 0;
}

Here is the warning message:

$ gcc -o fraction fraction.m -I /GNUstep/System/Library/Headers -L /GNUstep/System/Library/Libraries -lobjc -lgnustep-b
ase -fconstant-string-class=NSConstantString
fraction.m: In function 'main':
fraction.m:43:3: warning: 'Fraction' may not respond to '+new' [enabled by default]
fraction.m:43:3: warning: (Messages without a matching method signature [enabled by default]
fraction.m:43:3: warning: will be assumed to return 'id' and accept [enabled by default]
fraction.m:43:3: warning: '...' as arguments.) [enabled by default]
fraction.m:49:3: warning: 'Fraction' may not respond to '-free' [enabled by default]

Here is the output:

$ ./fraction.exe
The numerator is 0, and the denominator is 0

Not sure if it is relevant, but I am using GNUstep to compile it. I don't know of a way to run debug on it. I tried GDB but it did not work. By the way, do I need a Mac environment, if I want to take full advantage of this language's benefits? Are there non-Mac / apple applications of this language?

Upvotes: 1

Views: 79

Answers (1)

Sulthan
Sulthan

Reputation: 130200

Object is a deprecated root class. Use NSObject instead. Don't call free. If you are using manual memory management, use release. If you are using ARC, then don't call anything.

new doesn't have to be supported by GNUstep, so you should use the class alloc and init pattern.

@interface Fraction: NSObject
{
    int numerator;
    int denominator;
}
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;
-(void) print;
@end
//-----------------implementation section-------
@implementation Fraction;
//getters
-(int) numerator
{
    return numerator;
}
-(int) denominator
{
    return denominator;
}
//setters
-(void) setNumerator: (int) num
{
    numerator = num;
}
-(void) setDenominator: (int) denom
{
    denominator = denom;
}
-(void) print
{
    printf("The value of the fraction is %i/%i\n", numerator, denominator);
}
@end
//-----------------program section-------------------------
int main (void)
{
    Fraction *myFract = [[Fraction alloc] init];
    [myFract setNumerator: 1];
    [myFract setDenominator: 3];

    printf ("The numerator is %i, and the denominator is %i\n", [myFract numerator], [myFract denominator]);
    [myFract print];
    [myFract release];
    return 0;
}

You can also simplify with properties, the following is Obj-C 2.0 syntax with Automatic Reference Counting.

@interface Fraction: NSObject

@property (nonatomic, assign) int numerator;
@property (nonatomic, assign) int denominator;

- (void)print;

@end

@implementation Fraction
- (void)print {
   printf("The value of the fraction is %i/%i\n", self.numerator, self.denominator);
}
@end

int main (void)
{
    Fraction *myFract = [[Fraction alloc] init];
    myFract.numerator = 1;
    myFract.denominator = 3;

    printf ("The numerator is %i, and the denominator is %i\n", [myFract numerator], [myFract denominator]);
    [myFract print];
    return 0;
}

Even with GNUstep you should use Clang for compilation. GCC just won't do any more.

Upvotes: 1

Related Questions