c - 'struct <anonymous>' 没有成员命名

标签 c struct queue member abstract-data-type

为创建函数寻找引用节点并将其设置为空的方法。给定输出的任何建议都可以将队列中的前端和后端节点置为 NULL?

gcc -std=c99 -ggdb -Wall -Wextra  -c queueADT.c
queueADT.c:13:1: warning: useless storage class specifier in empty declaration [enabled by default]
 };
 ^
queueADT.c: In function 'que_create':
queueADT.c:36:6: error: 'struct <anonymous>' has no member named 'front'
   new->front = NULL;
      ^
queueADT.c:37:6: error: 'struct <anonymous>' has no member named 'rear'
   new->rear = NULL;
      ^
queueADT.c:38:8: error: expected identifier before '*' token
   new->*cmprFunc = NULL;

这是导致错误的代码的主要部分。 (2 个结构)

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


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 */
};


#include "queueADT.h"


/// create a queue that is either sorted by cmp or FIFO
//function with two void
QueueADT que_create( int (*cmp)(const void*a,const void*b) ) {

    QueueADT new;
    new = (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 );
}

最佳答案

这对我来说无效:

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

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

也许你需要这个:

    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;
        ...

:

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 */
};

应该是

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 */
};

而且这里的typedef是没有意义的,应该去掉:

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

其他问题:struct QueueADT 中的这两个声明在我看来很可疑:

    int *contents[ QUEUE_SIZE ];      /* number of items in queue  */
    int *cmprFunc;                    /* The compare function used for insert */

关于c - 'struct <anonymous>' 没有成员命名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26652992/

相关文章:

c - 基指针和栈指针到底是什么?他们指向什么?

pointers - Golang 使用反射在结构中设置 nil 字符串指针值

struct - 如何通过字符串名称设置结构字段值?

c++ - 如何从仅提供复制构造函数的类实例化 "empty"对象?

c# - 线程队列进程

php - 拦截应用程序的调用堆栈

gcc 编译器中的 C 变量声明 - 编译时错误

c - 来自输入文件的动态分配

c++ - 不初始化指针和将其初始化为空有什么区别?

python队列和多处理队列: how they behave?