简体   繁体   English

C Typedef - 不完整类型

[英]C Typedef - Incomplete Type

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 customer.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 customer.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 :) 提前致谢 :)

[PS if I forgot to mention something, let me know. [PS,如果我忘记提及的话,请告诉我。 Its been a stressful day for me, as you might tell ] 对你来说,这是一个充满压力的一天,你可能会告诉我们

Move the struct declaration to the header: 将struct声明移动到标题:

customer.h
typedef struct CustomerStruct
{
...
}

In C, the compiler needs to be able to figure out the size of any object that is referenced directly. 在C中,编译器需要能够计算出直接引用的任何对象的大小。 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 . 可以计算sizeof(CustomerNode)的唯一方法是,在构建customer_list.c时,编译器可以使用Customer的定义。

The solution is to move the definition of the struct from customer.c to customer.h . 解决方案是将结构的定义从customer.ccustomer.h

What you have is a forward declaration of Customer structure that you are trying to instantiate. 您拥有的是您尝试实例化的Customer结构的前向声明。 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. 因此,您需要做的是将源文件中的定义移动到标题中。

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->xxx in the header. 只要您不需要在标题中使用baz->xxx ,它就会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM