typedef 和 struct 之间的类型冲突

标签 c types linked-list

我有一个小问题,我在文件“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]

最佳答案

关于冲突类型错误,原因如下:

标题:

COMDS_RETURN *comds_slist_init( /* args */ );

来源:

COMDS_RETURN comds_slist_init( /* args */ ) { /* def */ }

一个文件中的返回类型为 COMDS_RETURN *,另一个文件中的返回类型为 COMDS_RETURN

关于typedef 和 struct 之间的类型冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28342935/

相关文章:

C# 字典在另一个字典中使用 TValue

typescript - 如何从 Typescript 中固定对象的键创建映射类型

haskell - 应用参数可以是 Ints 或 Doubles 的函数

java - 将整数添加到 ArrayList<Integer>

C:如何使 ArrayList 实现与 UNICODE_STRING 数据兼容?

c - 在 C 中将多个参数发送到 system()

c - 为什么设置 AI_PASSIVE 时 getaddrinfo() 返回第一个 IPv4 而不是 IPv6?

c - 如何检测已删除的 mmap 文件的读取?

java - 在常数时间内连接两个 java.util.LinkedList

C++有序链表搜索函数算法逻辑