2012-05-06 6 views
0

私は客観的なcを学ぼうとしています。'struct myStruct *'から 'struct myStruct *'に代入される互換性のないポインタ型

これはすべて私の.mファイル

@interface TetrisEngine() 
@property (nonatomic, readwrite) struct TetrisPiece *currPiece; 
@end 

struct TetrisPiece { 
    int name; 
    struct { 
     int colOff, rowOff; 
    } offsets[TetrisPieceRotations][TetrisPieceBlocks]; 
}; 

この隣の男の内容が適切でないはずです。私は、戻り値はあなたが

static struct TetrisPiece pieces[TetrisNumPieces] = {...}; 

@implementation TetrisEngine 
@synthesize currPiece; 

- (void) nextPiece 
    currPiece = &pieces[ ((random() % (TetrisNumPieces * 113)) + 3) % TetrisNumPieces]; 

を手助けするために見る必要があるすべてであると仮定し、私はエラーを取得する場所です:互換性のないポインタ型は、「構造体TetrisPiece *」

から「構造体TetrisPiece *」に割り当てます
+1

言語でOO機能がある場合は、ここでstructsを使用する理由は何ですか? –

答えて

4

ファイルvarが...

@interface TetrisEngine() { 
    // added curly braces and this 
    struct TetrisPiece *currPiece; 
} 

@property (nonatomic, readwrite) struct TetrisPiece *currPiece; 
@end 

残りがあるとして動作するはずのように、C型ポインタのために明示的に宣言する必要があります。私は構造体を宣言するためのより現代的な方法があると答えています。

+0

追加 - 実装ファイルで@synthesizeを使用する場合は、プロパティに対して宣言ファイル(.h)(struct TetrisPiece * currPiece)に追加する必要はありません。合成されたプロパティの名前と一致するインスタンス変数がない場合は、自動的に作成されます。 –

+0

ありがとうございます。これは構造体へのC型ポインタの場合にのみ当てはまりますか?言い換えれば、残っている目的型変数のこの明示的な宣言をスキップすることができますか? – austin

+0

はい、@iluvatar_GRのようです。オブジェクトポインタの場合、@ synthesizeは暗黙のうちに宣言を行います。 – danh

関連する問題