typedef 和 struct 之间的冲突类型
conflicting type between typedef and struct
我有一个小问题,我在我的文件 "single_list.h" 中用 typedef 声明了一个类型,并在 "single_list.c" 中实现了这个类型的结构,但是我遇到了一堆冲突的类型,我不明白为什么:
singe_list.h
typedef struct single_list_node SingleListNode;
typedef struct single_list SingleList;
COMDS_RETURN *comds_slist_init(SingleList **list, void (*destroy)(void *data),
int (*compare)(const void *fdata, const void *sdata));
void comds_slist_destroy(SingleList *list);
COMDS_RETURN comds_slist_insert_next(SingleList *list, SingleListNode *node, void *data);
COMDS_RETURN comds_slist_remove(SingleList *list, void **data);
COMDS_RETURN comds_slist_remove_next(SingleList *list,
SingleListNode *node, void ** data);
COMDS_RETURN comds_slist_find(const SingleList *list,
SingleList **out, const void *data);
single_list.c
#include <stdlib.h>
#include "error.h"
#include "single_list.h"
struct single_list_node {
void *data;
struct SingleListNode_ *nextNode;
};
struct single_list {
SingleListNode *head;
SingleListNode *tail;
size_t size;
void (*destroy)(void *data);
int (*compare)(const void *fdata, const void *sdata);
};
COMDS_RETURN comds_slist_init(SingleList **list, void (*destroy)(void *data),
int (*compare)(const void *fdata, const void *sdata))
{
if((*list = malloc(sizeof *list)) == NULL)
return COMDS_ALLOC_FAIL;
*list->head = NULL;
*list->tail = NULL;
*list->size = 0;
*list->destroy = destroy;
*list->compare = compare;
return COMDS_SUCESS;
}
void comds_slist_destroy(SingleList *list)
{
void *data;
if(list->destroy)
while(list->head)
{
comds_slist_remove_next(list, NULL, &data);
list->destroy(data);
}
free(list);
}
COMDS_RETURN comds_slist_insert_next(SingleList *list, SingleListNode *node,
void *data)
{
SingleListNode *addnode = malloc(sizeof *addnode);
if(addnode == NULL)
return COMDS_ALLOC_FAIL;
addnode->data = data;
if(node == NULL) {
addnode->nextNode = list->head;
node = list->head = addnode; // using node for checking list tail
}
else {
addnode->nextNode = node->nextNode;
node->nextNode = addnode;
}
// updating list->tail if needed
if(node == list->tail)
list->tail = addnode;
list->size++;
return COMDS_SUCESS;
}
COMDS_RETURN comds_slist_remove(SingleList *list, void **data)
{
SingleListNode *node, *prevNode = NULL;
if(list->compare)
for(node = list->head; node != NULL &&
list->compare && list->compare(*data, node->data) != 0;
prevNode = node, node = node->nextNode);
else
for(node = list->head; node != NULL &&
node->data != *data;
prevNode = node, node = node->nextNode);
if(node != NULL) {
return comds_slist_remove_next(list, prevNode, data);
}
return COMDS_ELEMENT_NOT_FOUND;
}
COMDS_RETURN comds_slist_remove_next(SingleList *list, SingleListNode *node, void **data)
{
SingleListNode *remNode;
// the list still not contain any node
if(list->size == 0 || (node && node->nextNode == NULL))
return COMDS_ELEMENT_NOT_FOUND;
if(node == NULL) {
*data = list->head->data;
remNode = list->head;
list->head = list->head->nextNode;
}
else {
*data = node->nextNode->data;
remNode = node->nextNode;
node->nextNode = node->nextNode->nextNode;
}
if(remNode == list->tail)
list->tail = node;
free(remNode);
list->size--;
return COMDS_SUCESS;
}
// iterate over the list node per node until finding the right node
// compare function must return 0 if the two data are the same
// if the function compare is 'NULL', a pointer comparaison is done
COMDS_RETURN comds_slist_find(const SingleList *list, SingleList **out,
const void *data)
{
if(list->size == 0)
return COMDS_ELEMENT_NOT_FOUND;
SingleListNode *node = list->head;
if(list->compare)
while(node != NULL && list->compare(node->data, data) != 0)
node = node->nextNode;
else
while(node != NULL && node->data != data)
node = node->nextNode;
*out = node;
return COMDS_SUCESS;
}
错误
single_list.c:19:14: error: conflicting types for ‘comds_slist_init’
In file included from single_list.c:4:0:
single_list.h:23:15: note: previous declaration of ‘comds_slist_init’ was here
single_list.c: In function ‘comds_slist_init’:
single_list.c:26:7: error: request for member ‘head’ in something not a structure or union
single_list.c:27:7: error: request for member ‘tail’ in something not a structure or union
single_list.c:28:7: error: request for member ‘size’ in something not a structure or union
single_list.c:29:7: error: request for member ‘destroy’ in something not a structure or union
single_list.c:30:7: error: request for member ‘compare’ in something not a structure or union
single_list.c: In function ‘comds_slist_insert_next’:
single_list.c:62:21: warning: assignment from incompatible pointer type [enabled by default]
single_list.c:67:18: warning: assignment from incompatible pointer type [enabled by default]
single_list.c: In function ‘comds_slist_remove’:
single_list.c:86:26: warning: assignment from incompatible pointer type [enabled by default]
single_list.c:90:26: warning: assignment from incompatible pointer type [enabled by default]
single_list.c: In function ‘comds_slist_remove_next’:
single_list.c:110:14: warning: assignment from incompatible pointer type [enabled by default]
single_list.c:113:25: error: dereferencing pointer to incomplete type
single_list.c:114:11: warning: assignment from incompatible pointer type [enabled by default]
single_list.c:116:34: error: dereferencing pointer to incomplete type
single_list.c: In function ‘comds_slist_find’:
single_list.c:142:9: warning: assignment from incompatible pointer type [enabled by default]
single_list.c:145:9: warning: assignment from incompatible pointer type [enabled by default]
single_list.c:147:7: warning: assignment from incompatible pointer type [enabled by default]
关于conflicting types
错误,原因如下:
Header:
COMDS_RETURN *comds_slist_init( /* args */ );
来源:
COMDS_RETURN comds_slist_init( /* args */ ) { /* def */ }
return 类型在一个文件中为 COMDS_RETURN *
,在另一个文件中为 COMDS_RETURN
。
我有一个小问题,我在我的文件 "single_list.h" 中用 typedef 声明了一个类型,并在 "single_list.c" 中实现了这个类型的结构,但是我遇到了一堆冲突的类型,我不明白为什么:
singe_list.h
typedef struct single_list_node SingleListNode;
typedef struct single_list SingleList;
COMDS_RETURN *comds_slist_init(SingleList **list, void (*destroy)(void *data),
int (*compare)(const void *fdata, const void *sdata));
void comds_slist_destroy(SingleList *list);
COMDS_RETURN comds_slist_insert_next(SingleList *list, SingleListNode *node, void *data);
COMDS_RETURN comds_slist_remove(SingleList *list, void **data);
COMDS_RETURN comds_slist_remove_next(SingleList *list,
SingleListNode *node, void ** data);
COMDS_RETURN comds_slist_find(const SingleList *list,
SingleList **out, const void *data);
single_list.c
#include <stdlib.h>
#include "error.h"
#include "single_list.h"
struct single_list_node {
void *data;
struct SingleListNode_ *nextNode;
};
struct single_list {
SingleListNode *head;
SingleListNode *tail;
size_t size;
void (*destroy)(void *data);
int (*compare)(const void *fdata, const void *sdata);
};
COMDS_RETURN comds_slist_init(SingleList **list, void (*destroy)(void *data),
int (*compare)(const void *fdata, const void *sdata))
{
if((*list = malloc(sizeof *list)) == NULL)
return COMDS_ALLOC_FAIL;
*list->head = NULL;
*list->tail = NULL;
*list->size = 0;
*list->destroy = destroy;
*list->compare = compare;
return COMDS_SUCESS;
}
void comds_slist_destroy(SingleList *list)
{
void *data;
if(list->destroy)
while(list->head)
{
comds_slist_remove_next(list, NULL, &data);
list->destroy(data);
}
free(list);
}
COMDS_RETURN comds_slist_insert_next(SingleList *list, SingleListNode *node,
void *data)
{
SingleListNode *addnode = malloc(sizeof *addnode);
if(addnode == NULL)
return COMDS_ALLOC_FAIL;
addnode->data = data;
if(node == NULL) {
addnode->nextNode = list->head;
node = list->head = addnode; // using node for checking list tail
}
else {
addnode->nextNode = node->nextNode;
node->nextNode = addnode;
}
// updating list->tail if needed
if(node == list->tail)
list->tail = addnode;
list->size++;
return COMDS_SUCESS;
}
COMDS_RETURN comds_slist_remove(SingleList *list, void **data)
{
SingleListNode *node, *prevNode = NULL;
if(list->compare)
for(node = list->head; node != NULL &&
list->compare && list->compare(*data, node->data) != 0;
prevNode = node, node = node->nextNode);
else
for(node = list->head; node != NULL &&
node->data != *data;
prevNode = node, node = node->nextNode);
if(node != NULL) {
return comds_slist_remove_next(list, prevNode, data);
}
return COMDS_ELEMENT_NOT_FOUND;
}
COMDS_RETURN comds_slist_remove_next(SingleList *list, SingleListNode *node, void **data)
{
SingleListNode *remNode;
// the list still not contain any node
if(list->size == 0 || (node && node->nextNode == NULL))
return COMDS_ELEMENT_NOT_FOUND;
if(node == NULL) {
*data = list->head->data;
remNode = list->head;
list->head = list->head->nextNode;
}
else {
*data = node->nextNode->data;
remNode = node->nextNode;
node->nextNode = node->nextNode->nextNode;
}
if(remNode == list->tail)
list->tail = node;
free(remNode);
list->size--;
return COMDS_SUCESS;
}
// iterate over the list node per node until finding the right node
// compare function must return 0 if the two data are the same
// if the function compare is 'NULL', a pointer comparaison is done
COMDS_RETURN comds_slist_find(const SingleList *list, SingleList **out,
const void *data)
{
if(list->size == 0)
return COMDS_ELEMENT_NOT_FOUND;
SingleListNode *node = list->head;
if(list->compare)
while(node != NULL && list->compare(node->data, data) != 0)
node = node->nextNode;
else
while(node != NULL && node->data != data)
node = node->nextNode;
*out = node;
return COMDS_SUCESS;
}
错误
single_list.c:19:14: error: conflicting types for ‘comds_slist_init’
In file included from single_list.c:4:0:
single_list.h:23:15: note: previous declaration of ‘comds_slist_init’ was here
single_list.c: In function ‘comds_slist_init’:
single_list.c:26:7: error: request for member ‘head’ in something not a structure or union
single_list.c:27:7: error: request for member ‘tail’ in something not a structure or union
single_list.c:28:7: error: request for member ‘size’ in something not a structure or union
single_list.c:29:7: error: request for member ‘destroy’ in something not a structure or union
single_list.c:30:7: error: request for member ‘compare’ in something not a structure or union
single_list.c: In function ‘comds_slist_insert_next’:
single_list.c:62:21: warning: assignment from incompatible pointer type [enabled by default]
single_list.c:67:18: warning: assignment from incompatible pointer type [enabled by default]
single_list.c: In function ‘comds_slist_remove’:
single_list.c:86:26: warning: assignment from incompatible pointer type [enabled by default]
single_list.c:90:26: warning: assignment from incompatible pointer type [enabled by default]
single_list.c: In function ‘comds_slist_remove_next’:
single_list.c:110:14: warning: assignment from incompatible pointer type [enabled by default]
single_list.c:113:25: error: dereferencing pointer to incomplete type
single_list.c:114:11: warning: assignment from incompatible pointer type [enabled by default]
single_list.c:116:34: error: dereferencing pointer to incomplete type
single_list.c: In function ‘comds_slist_find’:
single_list.c:142:9: warning: assignment from incompatible pointer type [enabled by default]
single_list.c:145:9: warning: assignment from incompatible pointer type [enabled by default]
single_list.c:147:7: warning: assignment from incompatible pointer type [enabled by default]
关于conflicting types
错误,原因如下:
Header:
COMDS_RETURN *comds_slist_init( /* args */ );
来源:
COMDS_RETURN comds_slist_init( /* args */ ) { /* def */ }
return 类型在一个文件中为 COMDS_RETURN *
,在另一个文件中为 COMDS_RETURN
。