c++ - 无法使用 typedef 结构解析字段 'tile'

标签 c++ c sdl game-engine

嘿伙计们,这不应该那么难,但为什么会这样

            fscanf(fp, "%d", Map.tile[x][y]);

tile 部分说 Field 'tile' could not be resolved 很抱歉遇到初学者问题,但我猜这应该是 char 到 int 转换的问题。 我将如何解决这个问题? 谢谢, waco001

void MapManager::loadMap(char *name){
    int x, y;
    FILE *fp;

    fp = fopen(name, "rb");
    const int MAX_MAP_Y = 32;
    const int MAX_MAP_X = 32;
    typedef struct Map
    {
        int tile[MAX_MAP_Y][MAX_MAP_X];
        char xs;
    } Map;
    /* If we can't open the map then exit */

    if (fp == NULL)
    {
        printf("Failed to open map %s\n", name);

        exit(1);
    }

    /* Read the data from the file into the map */

    for (y=0;y<MAX_MAP_Y;y++)
    {
        for (x=0;x<MAX_MAP_X;x++)
        {
            fscanf(fp, "%d", Map.tile[x][y]);
        }
    }

    /* Close the file afterwards */

    fclose(fp);
}

最佳答案

Map 是一种类型,但您需要一个对象。例如,您可以使用

Map map;
// ...
if (fscanf(fp, "%d", map.tile[x][y]) != 1) {
    fprintf(stderr, "ERROR: failed to read map.tile[%d][%d]\n", x, y);
}

关于c++ - 无法使用 typedef 结构解析字段 'tile',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19646947/

相关文章:

c++ - 在opencv中使用静态库而不是动态库

c - 如何找到C语言代码的执行时间?

c - 在 C : Writing to a dynamic memory buffer 中使用 Malloc

c - 'busy-wait'无限循环和条件验证无限循环的区别

c++ - 使用 time.h 时出现段错误

c++ - 如何获得从负无穷大到无穷大的范围,以 0 到 1 的范围表示?

c++ - 陷入构建游戏引擎的困境

c - 我的函数中的格式代码

c++ - 套接字 : send() function returned 'Broken Pipe' error

c - 如何在C中创建文件夹(需要在Linux和Windows上运行)