【问题标题】:array type has incomplete element type error数组类型有不完整的元素类型错误
【发布时间】:2018-10-24 08:37:15
【问题描述】:

我正在尝试编译一个程序,并且相当缺乏经验(边走边自学)。我正在努力解决的一件事是:

overland.h:62:37: 错误:数组类型的元素类型“struct sect_color_type”不完整

extern const struct sect_color_type sect_show[];

const struct sect_color_type sect_show[] = {
/*   Sector Type        Color   Symbol Description  Passable?   Move  R  G  B   */

{ SECT_INDOORS,       "&x", " ", "indoors",         FALSE,  1,  0, 0, 0 },
{ SECT_CITY,         "&Y", ":", "city",         TRUE,   1,      255, 128, 64 },
{ SECT_FIELD,        "&G", "+", "field",        TRUE,   1,  141, 215, 1 },
{ SECT_FOREST,       "&g", "+", "forest",       TRUE,   2,  0, 108, 47 },
{ SECT_HILLS,        "&O", "^", "hills",        TRUE,   3,  140, 102, 54 },
{ SECT_MOUNTAIN,     "&w", "^", "mountain",     TRUE,   5,  152, 152, 152 },
{ SECT_WATER_SWIM,   "&C", "~", "shallow water",    TRUE,   2,  89, 242, 251 },
{ SECT_WATER_NOSWIM, "&B", "~", "deep water",   TRUE,   2,  67, 114, 251 },
{ SECT_AIR,          "&x", "?", "air",      FALSE,  1,  0, 0, 0 },
{ SECT_UNDERWATER,   "&x", "?", "underwater",   FALSE,  5,  0, 0, 0 },
{ SECT_DESERT,       "&Y", "~", "desert",       TRUE,   3,  241, 228, 145 },
{ SECT_RIVER,        "&B", "~", "river",        TRUE,   3,  0, 0, 255 },
{ SECT_OCEANFLOOR,   "&x", "?", "ocean floor",  FALSE,  4,  0, 0, 0 },
{ SECT_UNDERGROUND,  "&x", "?", "underground",  FALSE,  3,  0, 0, 0 },
{ SECT_JUNGLE,       "&g", "*", "jungle",       TRUE,   2,  70, 149, 52 },
{ SECT_SWAMP,        "&g", "~", "swamp",        TRUE,   3,  218, 176, 56 },
{ SECT_TUNDRA,       "&C", "-", "tundra",       TRUE,   2,  54, 255, 255 },
{ SECT_ICE,          "&W", "=", "ice",      TRUE,   3,  133, 177, 252 },
{ SECT_OCEAN,        "&b", "~", "ocean",        FALSE,  1,  0, 0, 128 },
{ SECT_LAVA,         "&R", ":", "lava",         FALSE,  2,  245, 37, 29 },
{ SECT_SHORE,        "&Y", ".", "shoreline",    TRUE,   3,  255, 255, 0 },
{ SECT_TREE,         "&g", "^", "impass forest",    FALSE,  10, 0, 64, 0 },
{ SECT_STONE,        "&W", "^", "impas mountain", FALSE,    10, 128, 128, 128 },
{ SECT_QUICKSAND,    "&g", "%", "quicksand",    FALSE,  10,     128, 128, 0 },
{ SECT_WALL,         "&P", "I", "wall",         FALSE,  10, 255, 0, 255 },
{ SECT_GLACIER,      "&W", "=", "glacier",      FALSE,  10,     141, 207, 244 },
{ SECT_EXIT,         "&W", "#", "exit",         TRUE,   1,  255, 255, 255 },
{ SECT_TRAIL,        "&O", ":", "trail",        TRUE,   1,  128, 64, 0 },
{ SECT_BLANDS,   "&r", ".", "blasted lands",    TRUE,   2,  128, 0, 0 },
{ SECT_GRASSLAND,    "&G", ".", "grassland",    TRUE,   1,  83, 202, 2 },
{ SECT_SCRUB,        "&g", ".", "scrub",        TRUE,   2,  123, 197, 112 },
{ SECT_BARREN,       "&O", ".", "barren",       TRUE,   2,  192, 192, 192 },
{ SECT_BRIDGE,   "&P", ":", "bridge",       TRUE,   1,  255, 0, 128 },
#ifdef DRAGONFLIGHT
{ SECT_ROAD,         "&Y", ":", "road",         TRUE,   1,  215, 107, 0 },
{ SECT_LANDING,  "&R", "#", "landing",      TRUE,       1,  255, 0, 0 }
#else
{ SECT_ROAD,         "&Y", ":", "road",         TRUE,   1,  215, 107, 0 }
#endif
};

任何帮助将不胜感激。

编辑:我在 overland.h 中找到了定义

struct sect_color_type
{
sh_int sector;  /* Terrain sector */
char * color;   /* Color to display as */
char * symbol;  /* Symbol you see for the sector */
char * desc;    /* Description of sector type */
bool canpass;   /* Impassable terrain */
int move;             /* Movement loss */
sh_int graph1;  /* Color numbers for graphic conversion */
sh_int graph2;
sh_int graph3;
};

【问题讨论】:

  • 您正在创建一个数组sect_show[] of sect_color_type,但它还没有看到结构的定义。这是在哪里定义的?
  • 你需要在头文件中包含sect_color_type的声明。

标签: c arrays incomplete-type


【解决方案1】:

C 中的结构不像 Python 或其他语言中的字典。结构是专门定义的具有命名和大小字段的分组内存块。您将结构视为 collection,但它们不是。

在初始化数组之前,您需要在某处定义一个结构,如下所示:

typedef struct
{
    int sectorType;
    char* Color;
    char Symbol; // you seem to only need a single character for this field according to your example.
    char* Description;
    int Passable;
    int Move;
    int rgb[3];

} sect_color_type;

注意:我将 rgb 移到了它自己的结构内的整数数组中。

【讨论】:

  • 我发现在头文件的底部有一个定义。我已将其添加到我的原始帖子中。
  • @AndrewEly 是同一个头文件中的结构数组和结构定义吗?如果是这样,将结构定义移到数组上方。
  • 结构体的定义在头部底部,外部在顶部。
  • 这可能成功了!我还有更多代码要修改,但这次没有出现错误。
  • 确实如此。谢谢你,乔希,你的正确答案,也谢谢你,博士。游戏运行良好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-19
  • 2014-12-27
  • 2014-01-31
相关资源
最近更新 更多