C语言 C Typedef - 不完整的类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13634495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C Typedef - Incomplete Type
提问by Zyyk Savvins
So, out of the blue, the compiler decides to spit this in face: "field customer has incomplete type".
所以,出乎意料的是,编译器决定直截了当地吐槽:“字段客户的类型不完整”。
Here's the relevant snippets of code:
这是相关的代码片段:
customer.c
客户.c
#include <stdlib.h>
#include <string.h>
#include "customer.h"
struct CustomerStruct;
typedef struct CustomerStruct
{
char id[8];
char name[30];
char surname[30];
char address[100];
} Customer ;
/* Functions that deal with this struct here */
customer.h
客户.h
A header file for customer.h
customer.h 的头文件
#include <stdlib.h>
#include <string.h>
#ifndef CUSTOMER_H
#define CUSTOMER_H
typedef struct CustomerStruct Customer;
/* Function prototypes here */
#endif
This is where my problem is:
这是我的问题所在:
customer_list.c
customer_list.c
#include <stdlib.h>
#include <string.h>
#include "customer.h"
#include "customer_list.h"
#include "..\utils\utils.h"
struct CustomerNodeStruct;
typedef struct CustomerNodeStruct
{
Customer customer; /* Error Here*/
struct CustomerNodeStruct *next;
}CustomerNode;
struct CustomerListStruct;
typedef struct CustomerListStruct
{
CustomerNode *first;
CustomerNode *last;
}CustomerList;
/* Functions that deal with the CustomerList struct here */
This source file has a header file, customer_list.h ,but I don't think its relevant.
这个源文件有一个头文件 customer_list.h ,但我认为它不相关。
My Problem
我的问题
In customer_list.c, at the line with the comment /* Error Here */, the compiler complains about field customer has incomplete type.
在 customer_list.c 中,在注释行中/* Error Here */,编译器抱怨field customer has incomplete type.
I've been googling this problem all day, and now im at the point of pulling out my eyeballs and blending them with strawberries.
我一整天都在用谷歌搜索这个问题,现在我要掏出我的眼球并将它们与草莓混合。
What is the source of this error ?
这个错误的根源是什么?
Thanks in advance :)
提前致谢 :)
[P.S. if I forgot to mention something, let me know. Its been a stressful day for me, as you might tell ]
[PS 如果我忘记提及某事,请告诉我。正如你可能会说的,这对我来说是充满压力的一天]
回答by cnicutar
Move the struct declaration to the header:
将结构声明移动到标题:
customer.h
typedef struct CustomerStruct
{
...
}
回答by user295691
In C, the compiler needs to be able to figure out the size of any object that is referenced directly. The only way that the sizeof(CustomerNode)can be computed is for the definition of Customer to be available to the compiler when it is building customer_list.c.
在 C 中,编译器需要能够计算出任何直接引用的对象的大小。sizeof(CustomerNode)可以计算的唯一方法是编译器在构建时可以使用 Customer 的定义customer_list.c。
The solution is to move the definition of the struct from customer.cto customer.h.
解决方案是将结构体的定义从 移动customer.c到customer.h。
回答by user295691
What you have is a forward declaration of Customerstructure that you are trying to instantiate. This is not really allowed because compiler has no idea about the structure layout unless it sees it definition. So what you have to do is move your definition from the source file into a header.
您拥有的是Customer您试图实例化的结构的前向声明。这实际上是不允许的,因为编译器不知道结构布局,除非它看到它的定义。因此,您要做的就是将您的定义从源文件移动到标题中。
回答by David Fernandez
It seems that something like
似乎像
typedef struct foo bar;
won't work without the definition in the header. But something like
没有标题中的定义将无法工作。但像
typedef struct foo *baz;
will work, as long as you don't need to use baz->xxxin the header.
只要您不需要baz->xxx在标题中使用,就可以使用。

