gpt4 book ai didi

c - 分配给类型结构时不兼容的类型

转载 作者:太空宇宙 更新时间:2023-11-04 06:34:02 26 4
gpt4 key购买 nike

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

在文件 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;
}

field 是在文件 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 是“字段”我得到了一个错误:

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

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

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

最佳答案

您需要您的 main 才能看到该函数的声明。

例如在主要

extern field block_get_field_coordinates(int x, int y);

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

关于c - 分配给类型结构时不兼容的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17007208/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com