gpt4 book ai didi

c - .h注释: previous declaration of 'QueueADT' was here typedef struct { } *QueueADT;

转载 作者:行者123 更新时间:2023-11-30 17:26:46 27 4
gpt4 key购买 nike

我想知道如何解决重新定义 typedef struct 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 (注意,这与 QueueADT 不同)未定义,您仍然可以使用指向它的指针。然后,在您的 C 文件中,您只需定义 struct QueueADT :

struct QueueADT
{
...
};

请注意缺少 typedefstruct定义。

关于c - .h注释: previous declaration of 'QueueADT' was here typedef struct { } *QueueADT;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26655679/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com