我想知道如何解决用于重新定义typedef结构QueueADT的错误。

错误讯息

gcc -std=c99 -ggdb -Wall -Wextra  -c queueADT.c
queueADT.c:22:3: error: 'QueueADT' redeclared as different kind of symbol
 }*QueueADT;
   ^
In file included from queueADT.c:9:0:
queueADT.h:23:21: note: previous declaration of 'QueueADT' was here
 typedef struct { } *QueueADT;
                     ^
queueADT.c:30:1: error: unknown type name 'QueueADT'
 QueueADT que_create( int (*cmp)(const void*a,const void*b) ) {
 ^
queueADT.c:30:10: error: conflicting types for 'que_create'
 QueueADT que_create( int (*cmp)(const void*a,const void*b) ) {
          ^
In file included from queueADT.c:9:0:
queueADT.h:44:10: note: previous declaration of 'que_create' was here
 QueueADT que_create( int (*cmp)(const void*a,const void*b) );
          ^


.h文件具有一个定义(如果未编译),它是queueADT和que_create

#ifndef _QUEUE_IMPL_
typedef struct { } *QueueADT;
#endif

QueueADT que_create( int (*cmp)(const void*a,const void*b) );


现在我不确定如何从.c文件中定义QueueADT结构和que_create

#ifndef _QUEUE_IMPL_
#include "queueADT.h"
struct node {
    void* data;
    //size_t num;
    struct node *next;
}node;


typedef struct QueueADT {
    struct node *front;                     /* pointer to front of queue */
    struct node *rear;                      /* pointer to rear of queue  */
    int *contents[ QUEUE_SIZE ];      /* number of items in queue  */
    int *cmprFunc;                    /* The compare function used for insert */
}*QueueADT;






QueueADT que_create( int (*cmp)(const void*a,const void*b) ) {

    struct QueueADT *new;
    new = (struct QueueADT*)malloc(sizeof(struct QueueADT));

    if (cmp == NULL) {
        //do I create a new pointer to the cmp_int64?
        new->front = NULL;
        new->rear = NULL;
        new->cmprFunc = NULL;

    } else {
        new->cmprFunc = &cmp;
        new->front = NULL;
        new->rear = NULL;
    }

    return ( new );
}


编辑1
错误消息来自

queueADT.c:22:3: error: 'QueueADT' redeclared as different kind of symbol
 }*QueueADT;
   ^
In file included from queueADT.c:9:0:
queueADT.h:23:21: note: previous declaration of 'QueueADT' was here
 typedef struct { } *QueueADT;
                     ^
queueADT.c:30:1: error: unknown type name 'QueueADT'
 QueueADT que_create( int (*cmp)(const void*a,const void*b) ) {
 ^




In file included from queueADT.c:8:0:
queueADT.h:44:1: error: unknown type name 'QueueADT'
 QueueADT que_create( int (*cmp)(const void*a,const void*b) );
 ^
queueADT.h:49:19: error: unknown type name 'QueueADT'
 void que_destroy( QueueADT queue );


#define QUEUE_SIZE  5
#include "queueADT.h"
#define _QUEUE_IMPL_

struct node {
    void* data;
    //size_t num;
    struct node *next;
}node;


typedef struct QueueADT {
    struct node *front;                     /* pointer to front of queue */
    struct node *rear;                      /* pointer to rear of queue  */
    //int *contents[ QUEUE_SIZE ];      /* number of items in queue  */
    int *cmprFunc;                    /* The compare function used for insert */
}*QueueADT;


QueueADT que_create( int (*cmp)(const void*a,const void*b) ) {

    struct QueueADT *new;
    new = (struct QueueADT*)malloc(sizeof(struct QueueADT));

    if (cmp == NULL) {
        //do I create a new pointer to the cmp_int64?
        new->front = NULL;
        new->rear = NULL;
        new->cmprFunc = NULL;

    } else {
        new->cmprFunc = &cmp;
        new->front = NULL;
        new->rear = NULL;
    }

    return ( new );
}

最佳答案

C文件的第一行

#ifndef _QUEUE_IMPL_


不正确,应该定义宏而不是对其进行测试:

#define _QUEUE_IMPL_




话虽这么说,但是有一种更简单的方法可以完成您想做的事情。您可以在标头中使用单行代码来代替预处理程序包装:

typedef struct QueueADT *QueueADT;


即使未定义struct QueueADT(N.B。与QueueADT不同),您仍然可以使用指向它的指针。然后,在C文件中,只需定义struct QueueADT

struct QueueADT
{
    ...
};


请注意,在typedef定义中缺少struct

10-08 00:52