我在编译时不断收到这个错误。有人能告诉我为什么会这样吗? 我在 board.c 中声明了这些结构:
struct point {
short int rank;
short int file;
};
struct pieces {
Point pawns[8];
Point Knights[2];
Point BBishop;
Point WBishop;
Point Rooks[2];
Point Queen;
Point King;
};
我在 board.h 中也有这些 typedef:
typedef struct point Point;
typedef struct pieces Pieces;
在主源文件 (chess.c) 中,我有声明:
Pieces White;
当我编译它时说:
chess.c: In function 'main':
chess.c:19:10: error: storage size of 'White'isn't known
我尝试将结构移至 board.h,效果很好。为什么当我在 board.c 中有结构时它不起作用?
gcc 编译器
最佳答案
编译 chess.c
时,它需要知道 Pieces
typedef 扩展到什么,以便处理用该类型声明的变量。
指针类型可以在不知道它指向的类型的完整定义的情况下进行处理,但是对象类型需要知道完整的定义,因为它们为变量分配空间。要知道需要多少空间,编译器需要知道结构成员是什么。
当编译器处理 chess.c
时,它只有源文件和它包含的任何文件中的信息。除非您在某处有 #include "board.c"
,否则在编译 chess.c
时结构定义不可用。将#include
与.c
文件一起使用通常是错误的,它通常只应与.h
文件一起使用。这就是结构定义及其对应的类型定义通常放在 .h
文件中的原因。
关于c - 错误 : storage size of 'name' isn't known,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50221496/