不兼容的指针类型从“结构MYSTRUCT *”

问题描述:

分配给“结构MYSTRUCT *”我想学习目标C。不兼容的指针类型从“结构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设施,为什么使用结构在这里? – 2012-05-06 04:36:13

文件VAR需要为C型指针,像这样明确声明...

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

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

其余的将正常运行是。虽然我同意另一个答案,即有更多现代方法来声明oo中的结构。

+0

另外 - 如果你使用@synthesize,在实现文件中,为你的财产就没有必要将其添加在声明文件(.H)(结构TetrisPiece * currPiece)。如果没有与综合属性的名称相匹配的实例变量,则会自动创建一个。 –

+0

谢谢。这只是c型指针结构的情况吗?换句话说,我是否允许跳过这个显式声明,用于剩余的Objective-C类型变量? – austin

+0

是的,就像@iluvatar_GR提到的一样。对于对象指针,@ synthesize将隐含地执行该声明。 – danh