さて、私はObjective-Cに少し慣れていませんが、OOPの原則にはかなり精通しています。これで私を惹きつけているのは構文だけだと思います。
自分の練習問題として、簡単なコマンドライン計算機を作成しようとしています。私は自分の本(Stephen KochanのObjective-Cのプログラミング:第3版)をチェックしながらこのプログラムを入力しましたが、多くのエラーが発生します。
// 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.)
これはおそらく些細なことだと思いますが、これをいくつかの例と照らし合わせて確認したところ、これらの問題の原因が何であるかを理解できないようです。
ありがとう!