I've been trying to find answers from previous posts of my same issue but it's not working out. Below is only a few of the links I've checked:
我一直试图从以前的同一问题的帖子中找到答案,但它没有成功。以下是我检查的一些链接:
"parameter has incomplete type" warning C typedef: parameter has incomplete type How to resolve "parameter has incomplete type" error?
“参数有不完整类型”警告C typedef:参数有不完整类型如何解决“参数有不完整类型”错误?
Code:
码:
#include "listADT.h"
#include "client.h"
#include <stdlib.h>
#include <stdio.h>
struct node {
ClientInfo *data; // added pointer here
struct node * next;
};
struct list_type {
struct node * front;
int size;
};
ListType create() {
ListType listptr = malloc(sizeof(struct list_type));
if (listptr != NULL) {
listptr->front = NULL;
listptr->size = 0;
}
return listptr;
}
void push(ListType listptr, ClientInfo item) { <--- error here
struct node *temp = malloc(sizeof(struct node));
if (temp != NULL) {
temp->data = item;
temp->next = listptr->front;
listptr->front = temp;
(listptr->size)++;
}
}
int is_empty(ListType l) {
return l->size == 0;
}
int size_is(ListType l) {
return l->size;
}
void make_empty(ListType listptr) {
struct node* current = listptr->front;
while (current->next != NULL) {
destroy(listptr);
current = current->next;
}
(listptr->size)--;
}
void destroy(ListType listptr) {
struct node *temp = malloc(sizeof(struct node));
temp = listptr->front;
listptr->front = listptr->front->next;
free(temp);
(listptr->size)--;
}
void delete(ListType listptr, ClientInfo item) { <--- error here
struct node* current = listptr->front;
struct node *temp = malloc(sizeof(struct node));
while (current-> data != item) {
temp = current;
current = current->next;
}
temp->next = current->next;
(listptr->size)--;
}
int is_full(ListType l) {
}
Here is what the struct ClientInfo contains in another c file:
以下是struct ClientInfo在另一个c文件中包含的内容:
typedef struct ClientInfo {
char id[5];
char name[30];
char email[30];
char phoneNum[15];
} ClientInfo;
And here is the error I'm getting:
这是我得到的错误:
listADT.c:41:40: error: parameter 2 (‘item’) has incomplete type
void push(ListType listptr, ClientInfo item) {
^
listADT.c:83:42: error: parameter 2 (‘item’) has incomplete type
void delete(ListType listptr, ClientInfo item) {
I'm absolutely lost at this point on how to fix it. Please let me know if there is any other info I need to include.
在这一点上我完全迷失了如何修复它。如果我需要包含任何其他信息,请告诉我。
EDIT PORTION |
编辑部分|
listADT.h:
listADT.h:
#ifndef LISTADT_H
#define LISTADT_H
typedef struct list_type *ListType;
typedef struct ClientInfo ClientInfo;
ListType create(void);
void destroy(ListType listP);
void make_empty(ListType listP);
int is_empty(ListType listP);
int is_full(ListType listP);
void push(ListType listP, ClientInfo item);
void delete(ListType listP, ClientInfo item);
void printl(ListType listP);
#endif
Error after changing ClientInfo item
to ClientInfo *item
:
将ClientInfo项更改为ClientInfo *项后出错:
listADT.h:12:6: note: expected ‘ClientInfo * {aka struct ClientInfo *}’
but argument is of type ‘ClientInfo {aka struct ClientInfo}’
void push(ListType listP, ClientInfo *item);
1 个解决方案
#1
2
typedef struct ClientInfo ClientInfo;
typedef struct ClientInfo ClientInfo;
This is a forward declaration - a declaration of an incomplete type that will later on get completed in your client.c file. However, this design with a forward declaration in a header file makes the contents of the struct private.
这是一个前向声明 - 一个不完整类型的声明,稍后将在您的client.c文件中完成。但是,这种在头文件中带有前向声明的设计使得结构的内容变为私有。
No other file in your program will know what the struct contains, nor will they be able to access members. For them, the struct will still be incomplete and therefore they can't declare variables of this struct type. They can however declare pointers to the struct.
程序中没有其他文件可以知道结构包含什么,也无法访问成员。对于它们,结构仍然是不完整的,因此它们不能声明此结构类型的变量。但是,它们可以声明指向结构的指针。
This is actually how you do private encapsulation of objects in C. The concept is called "opaque type" and is considered good OO design practice.
这实际上就是你如何在C中对对象进行私有封装。这个概念被称为“opaque类型”,被认为是优秀的OO设计实践。
What you can do to fix the problems, is to change all functions in "client.h" and "client.c" so that they work with ClientInfo*
pointers instead. Then every other file using ClientInfo
will have to use pointers. Since they won't be able to declare any object of that type, you'll have to provide a constructor (and destructor). For example:
你可以做些什么来解决问题,就是改变“client.h”和“client.c”中的所有函数,以便它们使用ClientInfo *指针代替。然后使用ClientInfo的每个其他文件都必须使用指针。由于它们无法声明该类型的任何对象,因此您必须提供构造函数(和析构函数)。例如:
ClientInfo* client_create (void)
{
return malloc(sizeof(ClientInfo));
}
#1
2
typedef struct ClientInfo ClientInfo;
typedef struct ClientInfo ClientInfo;
This is a forward declaration - a declaration of an incomplete type that will later on get completed in your client.c file. However, this design with a forward declaration in a header file makes the contents of the struct private.
这是一个前向声明 - 一个不完整类型的声明,稍后将在您的client.c文件中完成。但是,这种在头文件中带有前向声明的设计使得结构的内容变为私有。
No other file in your program will know what the struct contains, nor will they be able to access members. For them, the struct will still be incomplete and therefore they can't declare variables of this struct type. They can however declare pointers to the struct.
程序中没有其他文件可以知道结构包含什么,也无法访问成员。对于它们,结构仍然是不完整的,因此它们不能声明此结构类型的变量。但是,它们可以声明指向结构的指针。
This is actually how you do private encapsulation of objects in C. The concept is called "opaque type" and is considered good OO design practice.
这实际上就是你如何在C中对对象进行私有封装。这个概念被称为“opaque类型”,被认为是优秀的OO设计实践。
What you can do to fix the problems, is to change all functions in "client.h" and "client.c" so that they work with ClientInfo*
pointers instead. Then every other file using ClientInfo
will have to use pointers. Since they won't be able to declare any object of that type, you'll have to provide a constructor (and destructor). For example:
你可以做些什么来解决问题,就是改变“client.h”和“client.c”中的所有函数,以便它们使用ClientInfo *指针代替。然后使用ClientInfo的每个其他文件都必须使用指针。由于它们无法声明该类型的任何对象,因此您必须提供构造函数(和析构函数)。例如:
ClientInfo* client_create (void)
{
return malloc(sizeof(ClientInfo));
}