2012-02-19 5 views
0

コンパイル警告(1):不完全な実装Icomplete実装とポインタ変換エラー。

私は.hと.mファイルを比較し、whats宣言と実装の間に矛盾やスペルミスがないかどうかを確認しました。

コンパイラ警告(2):互換性のない整数からポインタへの変換 'int'型( 'int *')と 'int'型の式を送信しています。

私はアスタリスクを25分間何度も組み合わせていましたが、コンパイラはまだ不幸です。私のクラスのインスタンスを宣言して初期化され

#import "Game.h" 
#import "stdlib.h" 

const int MAXRAND = 15; 
const int MAXCOL = 7; 
const int MAXROW = 9; 

NSInteger gameState[MAXROW][MAXCOL]; 
NSInteger answerBoard[MAXROW][MAXCOL]; 

@implementation Game//compiler warning 1 


-(void)init:(NSInteger*) rows: (NSInteger*) columns: (NSInteger*) operators:(NSInteger*) operands{ 
    NSLog(@"init sent"); 
    numRows = *rows; 
    numColumns = *columns; 
    numOperators = *operators; 
    numOperands = *operands; 
    //seed random number generator 

    //generate rand nums for operands 
    int operandList[numOperands]; 
    for (int i = 0; i < numOperands; i++) { 
     srandom(time(NULL)); 
     operandList[i] = (random()%MAXRAND); 
    } 
    //generate state and answer board 
    BOOL gameState[numRows][numColumns]; 
    NSInteger answerBoard[numRows][numColumns];  
    for (int i = 0; i < numRows; i++) { 
     for (int j = 0; j < numColumns; j++) { 
      gameState[i][j] = NO; 
      answerBoard[i][j] = (operandList[random()%numOperands])+ 
      (operandList[random()%numOperands])- 
      (operandList[random()%numOperands]); 
     } 

    } 
} 

-(void)updateGame:(NSInteger*)enteredNum{ 
    NSLog(@"updateGame sent"); 
    for (int i = numColumns; i > 0; i--) { 
     for (int j = numRows; j > 0; j--) { 
      if (gameState[i][j] == NO){ 
       if (*enteredNum == answerBoard[i][j]){ 
        gameState[i][j] = YES; 
       } 
      } 

     } 
    } 
} 


@end//Game 

#import <Foundation/Foundation.h> 

@interface Game : NSObject 
{ 
    NSInteger numRows, numColumns, numOperators, numOperands; 
} 

-(void)init:(NSInteger*) rows: (NSInteger*) columns: (NSInteger*) operators:(NSInteger*) operands; 
-(void)updateGame:(NSInteger*) enteredNum; 

@end 

NSInteger *rows = 7, *columns = 6, *operators = 2, *operands = 6;//compiler warning 2 
Game *game = [Game new]; 
[game init:rows :columns :operators :operands]; 
+0

よろしくお願いします。Cのプログラミング言語の基本的な点については、ぜひご検討ください。 – ZhangChn

+0

テストプロジェクトでコードをコンパイルしようとしましたが、動作しています。プロジェクトをクリアしようとしました –

答えて

0
NSInteger *rows = 7, *columns = 6, *operators = 2, *operands = 6; 

rows,columns, operators, operandsはタイプNSInteger *です。メモリを割り当てて、それらが指しているメモリ位置に7,6,2,6を配置する必要があります。 Cの用語では、

int *ptr = 7; // Wrong. Because, ptr is pointing no where to keep 7 in that location. 
+0

最初はアスタリスクはありませんでしたが、別のエラーが発生しました。別の方法を提案したり、それを書くことはできますか?私は少し混乱しています。 –

0

試しましたか?

NSInteger rows = 7, columns = 6, operators = 2, operands = 6; 

とは何ですか?

[Game new]; 

コンパイラは、おそらくあなたが新しいという名前の関数を実装するために期待しています。

編集: NSIntegerからすべての「*」を削除してみます。

+1

"new"はちょうど[[Game alloc] init]と言っているだけです。http://stackoverflow.com/questions/719877/use-of-alloc-init-instead-of-new-objective-c –

+0

申し訳ありません、私は「新しい」ことを知らなかった。私はいつも 'alloc'と 'init'を使います。 –

+0

私はアスタリスクを削除しました。メソッド定義とObj-C文法では、ポインタを取る必要があります。それ以外の場合はエラーが発生します。だから私が必要なのは:NSIntegerの宣言と初期化、そしてそれへのポインタです。 –