2012-12-19 83 views
1

好吧,我对Objective-C很陌生,但我对OOP原理非常熟悉。我认为这只是让我感受到的语法。调试初学者的Objective-C程序

作为一个我自己的练习,我试图构建一个简单的命令行计算器。我在检查我的书的同时打字了这个程序(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.) 

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

谢谢!

+5

在您声明的所有方法中删除 - (float)add:(float)x,(float)y; ===> - (float)add:(float)x:(float)y; –

+1

我没有你提到的那本书,但是在查看了它的[目录](http://my.safaribooksonline.com/book/programming/objective-c/9780321712172)后,我建议你看看在第7章中名为“方法的多重参数”一节。 –

回答

4

你真的应该读一个目标C初学者教程...

你的方法声明是错误的。他们应该像

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

你不是逗号分隔的参数,尝试做一个“句子”,说明该方法做什么,在这种情况下,“添加X到Y”。