1

下面是 2 个结构的定义,然后是使用它们的简短方法体。我不明白为什么编译器会抛出错误:

physics.c:95:错误:分配中的类型不兼容

cpBody并且cpSpace是来自外部库的类型,这不是问题的一部分。

typedef struct gameBody gameBody;

struct gameBody
{
    cpBody *body;
    int numberOfShapes;
    cpShape *arrayOfShapes; //This stores an array of pointers to Shapes
};

//Struct that stores the cpSpace object and the array of pointers to the body objects
typedef struct gameSpace gameSpace;

struct gameSpace
{ 
    cpSpace *space;
    int numberOfObjects;
    gameBody *arrayOfObjects;       //This stores an array of gameBodys
};

void physicsAddBody(gameSpace *space, gameBody *body, int objectIndex)
{
    gameBody *array = space -> arrayOfObjects;
    array[objectIndex] = body; //THIS IS WHERE THE ERROR IS THROWN
}
4

2 回答 2

2

数组[objectIndex] = 身体;

左边是a gameBody,右边是a gameBody*

您是尝试复制结构还是要将指针分配给指针数组?

于 2013-02-17T08:35:29.580 回答
0

你的最后一行应该是:

array[objectIndex] = *body;

因为 body 不是 gameBody 而是指向 gameBody 的指针。

(我希望你已经在某处分配了内存......)

于 2013-02-17T08:35:15.460 回答