1

好的,我对 Objective-C 有点陌生,但我对 OOP 原则非常熟悉。我认为这只是让我明白这一点的语法。

作为我自己的练习,我正在尝试构建一个简单的命令行计算器。我在查看我的书(Stephen Kochan 的Objective-C 编程:第三版)时输入了这个程序,但它给我带来了很多错误。

// Command line calculator in Objective-C

#import <Foundation/Foundation.h>

// -------- Interface -------- //

@interface calc: NSObject{
    float x, y, result;
    char op;
}

- (float) add: (float) x, (float) y;
- (float) sub: (float) x, (float) y;
- (float) mul: (float) x, (float) y;
- (float) div: (float) x, (float) y;
+ (void) evaluate;

@end

// -------- Implementation -------- //

@implementation calc

    -(float) add: (float) x, (float) y{
    return x+y;
    }

    -(float) sub: (float) x, (float) y{
        return x-y;
    }

    -(float) mul: (float) x, (float) y{
        return x*y;
    }

    -(float) div: (float) x, (float) y{
        return x/y;
    }

    +(void) evaluate: (float) x, (char) op, (float) y{
        float result;
        switch(op){
            case '+':
                result = [add: x, y]; break;
            case '-':
                result = [sub: x, y]; break;
            case '*':
            case 'x':
                result = [mul: x, y]; break;
            case '/':
            case '÷':
                result = [div: x, y]; break;
        }
        NSLog(@"%s%f", "=", result);
    }

@end

// -------- Driver -------- //

int main(int argc, const char argv[]){
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    calc *cal = [[calc alloc] init];

    float x, y;
    char op;

    NSLog(@"%s", "Welcome to the calculator!\n Please enter a simple expresion (ex. 4+3)...");
    scanf("%f%c%f", &x, &op, &y);

    [cal evaluate: x, op, y];

    [pool drain];
}

我在 Mac 上,所以我用 编译它gcc -framework Foundation main.m -o calc,它抛出

calcBundle.m:14: error: expected declaration specifiers or ‘...’ before ‘(’ token
calcBundle.m:15: error: expected declaration specifiers or ‘...’ before ‘(’ token
calcBundle.m:16: error: expected declaration specifiers or ‘...’ before ‘(’ token
calcBundle.m:17: error: expected declaration specifiers or ‘...’ before ‘(’ token
calcBundle.m:26: error: expected declaration specifiers or ‘...’ before ‘(’ token
calcBundle.m:30: error: expected ‘{’ before ‘,’ token
calcBundle.m:34: error: expected declaration specifiers or ‘...’ before ‘(’ token
calcBundle.m:38: error: expected ‘{’ before ‘,’ token
calcBundle.m:42: error: expected declaration specifiers or ‘...’ before ‘(’ token
calcBundle.m:42: error: expected ‘{’ before ‘,’ token
calcBundle.m:53:9: warning: multi-character character constant
calcBundle.m:59: warning: incomplete implementation of class ‘calc’
calcBundle.m:59: warning: method definition for ‘+evaluate’ not found
calcBundle.m:59: warning: incomplete implementation of class ‘calc’
calcBundle.m:59: warning: method definition for ‘-div:’ not found
calcBundle.m:59: warning: method definition for ‘-mul:’ not found
calcBundle.m:59: warning: method definition for ‘-sub:’ not found
calcBundle.m:59: warning: method definition for ‘-add:’ not found
calcBundle.m: In function ‘main’:
calcBundle.m:73: warning: ‘calc’ may not respond to ‘-evaluate:’
calcBundle.m:73: warning: (Messages without a matching method signature
calcBundle.m:73: warning: will be assumed to return ‘id’ and accept
calcBundle.m:73: warning: ‘...’ as arguments.)

我知道这可能是微不足道的事情,但我已经对照几个例子进行了检查,似乎无法弄清楚这些问题的根源是什么。

谢谢!

4

1 回答 1

4

你真的应该阅读一个客观的C初学者教程......

您的方法声明是错误的。他们应该看起来像

-(float) add: (float) x to: (float) y
{
    return x+y;
}

您不使用逗号分隔参数,而是尝试创建一个“句子”来说明该方法的作用,在本例中为“将 x 添加到 y”。

于 2012-12-19T08:18:34.700 回答