0

我开始学习Objective-C并写课,但我的问题是我的课主要不认识

这是我的主要内容:

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{
    @autoreleasepool
    {
        Fraction *fraction = [[Fraction alloc] init];
    }

    return 0;
}

我的界面:

#import <Foundation/Foundation.h>

@interface Fraction : NSObject

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

@end

执行:

#import "Fraction.h"

@implementation Fraction
{
    int numerator;
    int denomerator;
}

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

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

-(void) setDenominator: (int) n
{
    denomerator = n;
}

-(int) numerator
{
    return  numerator;
}

-(int) denomerator
{
    return denomerator;
}

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

@end

当我写Fraction *fraction = [[Fraction alloc] init];我的错误信息是使用未声明的标识符分数

4

1 回答 1

1

在 main.m 中,您需要添加

#import "Fraction.h"

以便编译器知道分数。

于 2013-03-13T18:56:54.353 回答