【问题标题】:Incompatible pointer types assigning to 'struct myStruct *' from 'struct myStruct *'从“struct myStruct *”分配给“struct myStruct *”的不兼容指针类型
【发布时间】:2012-05-06 04:33:42
【问题描述】:

我正在努力学习目标 c。

这些都在我的 .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];

这就是我得到错误的地方:不兼容的指针类型从'struct TetrisPiece *'分配给'struct TetrisPiece *'

【问题讨论】:

  • 如果你有一种语言的 OO 工具,为什么在这里使用结构?

标签: objective-c ios incompatibletypeerror


【解决方案1】:

需要为 c 类型指针显式声明文件 var,如下所示...

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

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

其余的应该按原样工作。虽然我同意另一个答案,即在 oo 中声明结构有更现代的方法。

【讨论】:

  • addition - 如果你在实现文件上使用@synthesize,你的属性就不需要在声明文件 (.h)(struct TetrisPiece *currPiece) 上添加它。如果没有与合成属性名称匹配的实例变量,则会自动创建一个。
  • 谢谢。这仅仅是指向结构的 c 类型指针的情况吗?换句话说,我是否可以为剩余的 Objective-c 类型变量跳过这个显式声明?
  • 是的,就像提到的@iluvatar_GR。对于对象指针,@synthesize 会隐式声明。
  • 没有 c 类型指针和 object-c 类型指针。它们都是指针,它们只有一个内存地址作为值。您需要声明对象 (@interface) 的所有即时变量(指针或非指针),除非您使用 @synthesize。
  • 我必须恭敬地不同意。自己试试这个提问者的代码......编译器错误。用对象替换结构指针,并且(正如您正确指出的那样)@synthesize 将隐式声明。你是对的,c 类型的指针和对象在语法上非常相似。但它们并不相同,这里说明了一个语法差异。而且语法不是现实……结构描述的对象和字节根本不一样。干杯。
猜你喜欢
  • 2021-01-17
  • 2021-02-10
  • 2015-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-17
  • 1970-01-01
  • 2016-03-25
相关资源
最近更新 更多