分配给类型struct时不兼容的类型

时间:2013-06-09 06:54:11

标签: c

我正在写一个简单的游戏,我最近遇到的问题似乎并不难解决,但是我没有想法。

在文件blocks.c和其他函数中我定义了一个简单的函数:

field block_get_field_coordinates(int x, int y)
{
    field temp;

    temp.x = floor( x / 65 ) * 65 + 15;
    temp.y = floor( y / 65) * 65 + 15;
    return temp;
}

字段是在文件grid.h中声明的结构

struct field
{
    int x, y;
}f;

typedef struct field field;

在main.c中,当我尝试以下内容时:

f = block_get_field_coordinates(blocks[x][y].x4, blocks[x][x].y4);

如果f是'field',我收到错误:

incompatible types when assigning to type 'struct field' from type 'int'

我真的不知道我在这里做错了什么。 blocks.c中的其他函数可以正常工作而不会丢失任何错误。

有趣的是,当我只复制struct field的声明时,函数block_get_field_coordinates到单独的文件中,它起作用了。

1 个答案:

答案 0 :(得分:4)

您需要主人才能看到该功能的声明。

e.g。在主要

extern field block_get_field_coordinates(int x, int y);

或者更好地将它放在block.h中,因为main需要类型以及使用该类型的任何函数。

相关问题