&#39; struct <anonymous>&#39;没有名为</anonymous>的成员

时间:2014-10-30 12:28:15

标签: c struct queue member abstract-data-type

寻找引用节点的方法,并为创建函数将其设置为null。来自该给定输出的任何建议将队列中的前端节点和后节点置为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 );
}

2 个答案:

答案 0 :(得分:2)

这看起来对我无效:

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

答案 1 :(得分:0)

  1. typedef
  2. 中删除typedef struct node {..
  3. struct QueueADT {//body};更改为typedef struct {//body}QueueADT;
  4. QueueADT new;应该QueueADT *new;

    依旧......