我一直在尝试从以前的同一期刊中找到答案,但它没有成功。以下是我检查过的几个链接:
"parameter has incomplete type" warning C typedef: parameter has incomplete type How to resolve "parameter has incomplete type" error?
代码:
#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) {
}
以下是struct ClientInfo在另一个c文件中包含的内容:
typedef struct ClientInfo {
char id[5];
char name[30];
char email[30];
char phoneNum[15];
} ClientInfo;
这是我得到的错误:
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) {
关于如何修复它,我完全迷失了。如果我需要提供任何其他信息,请告诉我。
编辑部分|
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
将ClientInfo item
更改为ClientInfo *item
后错误:
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);
答案 0 :(得分:2)
typedef struct ClientInfo ClientInfo;
这是一个前向声明 - 一个不完整类型的声明,稍后将在您的client.c文件中完成。但是,这种在头文件中带有前向声明的设计使得结构的内容变为私有。
程序中没有其他文件会知道结构包含什么,也无法访问成员。对于他们来说,结构仍然是不完整的,因此他们不能声明这种结构类型的变量。但是,它们可以声明指向结构的指针。
这实际上是你如何在C中对对象进行私有封装。这个概念被称为&#34; opaque类型&#34;并被认为是良好的OO设计实践。
你可以做些什么来解决问题,就是改变&#34; client.h&#34;中的所有功能。和&#34; client.c&#34;这样他们就可以使用ClientInfo*
指针。然后使用ClientInfo
的每个其他文件都必须使用指针。由于他们无法声明该类型的任何对象,因此您必须提供构造函数(和析构函数)。例如:
ClientInfo* client_create (void)
{
return malloc(sizeof(ClientInfo));
}