调试初学者的Objective-C程序

时间:2012-12-19 08:14:42

标签: objective-c calculator

好的,我是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.)

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

谢谢!

1 个答案:

答案 0 :(得分:4)

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

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

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

你没有逗号分隔参数,你试图用一个“句子”来说明方法的作用,在这种情况下“将x添加到y”。