编译错误struct没有名为next和prev的成员

编译错误struct没有名为next和prev的成员,c,nodes,function-pointers,doubly-linked-list,abstract-data-type,C,Nodes,Function Pointers,Doubly Linked List,Abstract Data Type,嘿,我正在尝试实现一个通用列表迭代器,它可以存储任何类型的元素。它还有其他文件来处理正整数类型和字符串类型。但是,我收到一个错误,指出struct IteratorGRep没有名为“next”的成员,也没有名为“prev”的成员。节点next和prev都已声明,这应该可以正常工作,但不确定错误在哪里。我使用的是linux环境,错误在编译时指示。代码如下: #include <stdlib.h> #include <stdio.h> #include <assert.

嘿,我正在尝试实现一个通用列表迭代器,它可以存储任何类型的元素。它还有其他文件来处理正整数类型和字符串类型。但是,我收到一个错误,指出struct IteratorGRep没有名为“next”的成员,也没有名为“prev”的成员。节点next和prev都已声明,这应该可以正常工作,但不确定错误在哪里。我使用的是linux环境,错误在编译时指示。代码如下:

#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include "iteratorG.h"

typedef struct Node {

  char   *value;  // value of thee list item (string)
  struct Node *prev;
  // pointer previous node in list
  struct Node *next;
  // pointer to next node in list

  // implemented struct here .. 
} Node;

typedef struct IteratorGRep {

  int  numofit;      // count of items in list
  Node *head;      // first node in list
  Node *curr;       // current node in list
  Node *tail;       // last node in list

  ElmCompareFp  cmpElm;
  ElmNewFp  newElm;
  ElmFreeFp freeElm;

  // implemented struct here .. 

} IteratorGRep;


/*


  // functions below .... 
 */


IteratorG newIterator(ElmCompareFp cmpFp, ElmNewFp newFp, ElmFreeFp freeFp){

    struct IteratorGRep *it;

  it = malloc(sizeof (struct IteratorGRep));
  assert (it != NULL);
  it->numofit = 0;
  it->head = NULL;
  it->tail = NULL;
  it->curr = NULL;
  it->cmpElm=cmpFp;
  it->newElm=newFp;
  it->freeElm=freeFp;
  return it;

    // created list 
}

int  add(IteratorG it, void *vp){

  Node *temp=NULL;
  temp=it->next;
  it->next=vp;
  vp->prev=it;
  vp->next=temp;
  temp->prev=vp;

    // Inserts element pointed by 'vp' into list iterator 'it' 
    return 1;
}
int  hasNext(IteratorG it){

  if(it->next!=NULL)
   {
     return 1;
   }

    // check for any next elements  
    return 0;
}
int  hasPrevious(IteratorG it){

    if(it->prev!=NULL)
    {
      return 1;
    }
    // check for any previous elements 
    return 0;
}
void *next(IteratorG it){
  if(it->next!=NULL)
  {
    it=it->next;
    Node *curre=it->curr;
    return curre;

  }
    // move to next element
    return NULL;
}
void *previous(IteratorG it){

    if(it->prev!=NULL)
    {
      it=it->prev;
      Node *curre=it->curr;
      return curre;

    }

    // moves to previous element
    return NULL;
}
int  del(IteratorG it){
  if(it->prev!=NULL)
  {
    Node *temp_curr=it->curr;
    Node *temp_prev=it->prev->prev;
    temp_curr->prev=temp_prev;
    temp_prev->next=temp_curr;
    return 1;

  }
     // removes previous element from list 
  if(it->prev==NULL)
  {
    return 0;
  }
}
int  set(IteratorG it, void *vp){
  if(it->prev!=NULL)
  {

  Node *store_curr=it->curr;
  Node *store_prev=it->prev->prev;

  store_curr->prev=vp;
  vp->next=store_curr;
  store_prev->next=vp;
  vp->prev=store_prev;
  return 1;
  }
    // Replaces previous element with the element (*vp) 
    return 0;
}
IteratorG advance(IteratorG it, int n){


    // Advance by n times and return list with n times of elements
    // To  implement function here and change return value 
    return NULL;
}
void reverse(IteratorG it){
  Node *curr = it->head;
  Node *temp = NULL;
  while(curr != NULL) {
    temp = curr->next;
    curr->next = curr->prev;
    curr->prev = temp;
    curr = temp;
  }
  temp = it->head;
  it->head = it->tail;
  it->tail = temp;    

    // reverses order of list  
}
IteratorG find(IteratorG it, int (*fp) (void *vp) ){

    // finds elements after current position,append to new list
    // To implement function here and change return value 

     return NULL;
}

int distanceFromStart(IteratorG it){

  Node *c=it->curr;
  int count=0;

  if (c->prev==NULL){
    return 0;  
  }

  while(c->prev!=NULL)
  {
    c=c->prev;
    count++;
    return count;
  }


     // counts number of nodes from current position to start of list 

}
int distanceToEnd(IteratorG it){

  Node *cu=it->curr;
  int count=0;

  if (cu->next==NULL){
    return 0;  
  }
  while(cu->next!=NULL)
  {
    cu=cu->next;
    count++;
    return count;
  }
    // counts number of nodes from current position to end of list  

}
void reset(IteratorG it){

  it->curr=it->head;

    // reset to start of list  
    return;
}
void freeIt(IteratorG it){
  assert(it != NULL);
  Node *curr, *prev;
  curr = it->first;
  while (curr != NULL) {
    prev = curr;
    curr = curr->next;
    free(prev->value);
    free(prev);
  }
  free(it); 

    // remove nodes in it and free memory  

}
#ifndef LISTITERATORG_H
#define LISTITERATORG_H

#include <stdio.h>

typedef struct IteratorGRep *IteratorG;

typedef int   (*ElmCompareFp)(void const *e1, void const *e2);
typedef void *(*ElmNewFp)(void const *e1);
typedef void  (*ElmFreeFp)(void *e1);


IteratorG newIterator(ElmCompareFp cmpFp, ElmNewFp newFp, ElmFreeFp freeFp);
int  add(IteratorG it, void *vp);
int  hasNext(IteratorG it);
int  hasPrevious(IteratorG it);
void *next(IteratorG it);
void *previous(IteratorG it);
int  del(IteratorG it);
int  set(IteratorG it, void *vp);
IteratorG advance(IteratorG it, int n);
void reverse(IteratorG it);
IteratorG find(IteratorG it, int (*fp) (void *vp) );
int distanceFromStart(IteratorG it);
int distanceToEnd(IteratorG it);
void reset(IteratorG it);
void freeIt(IteratorG it);

#endif
#包括
#包括
#包括
#包括“iteratorG.h”
类型定义结构节点{
char*value;//列表项的值(字符串)
结构节点*prev;
//指针指向列表中的上一个节点
结构节点*下一步;
//指向列表中下一个节点的指针
//在这里实现了struct。。
}节点;
typedef结构IteratorGRep{
int numofit;//列表中项目的计数
Node*head;//列表中的第一个节点
Node*curr;//列表中的当前节点
Node*tail;//列表中的最后一个节点
elmp-cmpElm;
ElmNewFp-newElm;
榆树;
//在这里实现了struct。。
}IteratorGRep;
/*
//以下功能。。。。
*/
IteratorG newIterator(elmcomarefp cmpFp、ElmNewFp newFp、ElmFreeFp freeFp){
结构IteratorGRep*it;
it=malloc(sizeof(struct IteratorGRep));
断言(it!=NULL);
it->numofit=0;
it->head=NULL;
it->tail=NULL;
it->curr=NULL;
it->cmpElm=cmpFp;
it->newElm=newFp;
it->freeElm=freeFp;
归还它;
//创建的列表
}
int add(iteratorgit,void*vp){
节点*temp=NULL;
temp=it->next;
it->next=vp;
vp->prev=it;
vp->next=温度;
temp->prev=vp;
//将“vp”指向的元素插入列表迭代器“it”
返回1;
}
int hasNext(iteratorgit){
if(it->next!=NULL)
{
返回1;
}
//检查下一个元素
返回0;
}
int hasPrevious(IteratorgIt){
如果(it->prev!=NULL)
{
返回1;
}
//检查以前的元素
返回0;
}
void*next(迭代组织它){
if(it->next!=NULL)
{
it=it->next;
节点*curre=it->curr;
返回电流;
}
//移动到下一个元素
返回NULL;
}
void*previous(迭代组织它){
如果(it->prev!=NULL)
{
it=it->prev;
节点*curre=it->curr;
返回电流;
}
//移动到上一个元素
返回NULL;
}
int del(迭代组织it){
如果(it->prev!=NULL)
{
节点*temp\u curr=it->curr;
节点*temp_prev=it->prev->prev;
当前温度->当前温度=当前温度;
临时上一步->下一步=临时当前;
返回1;
}
//从列表中删除上一个元素
如果(it->prev==NULL)
{
返回0;
}
}
int集(iteratorgit,void*vp){
如果(it->prev!=NULL)
{
节点*store_curr=it->curr;
节点*store_prev=it->prev->prev;
存储当前->当前=vp;
vp->next=门店\当前;
存储上一个->下一个=vp;
vp->prev=存储\预存;
返回1;
}
//用元素(*vp)替换上一个元素
返回0;
}
IteratorgAdvance(IteratorgIT,intn){
//前进n次,返回n次元素列表
//在此处实现函数并更改返回值
返回NULL;
}
无效反向(迭代组织它){
节点*curr=it->head;
节点*temp=NULL;
while(curr!=NULL){
温度=当前->下一步;
当前->下一个=当前->上一个;
当前->当前=温度;
电流=温度;
}
温度=it->head;
它->头=它->尾;
it->tail=temp;
//颠倒列表的顺序
}
IteratorG find(IteratorG it,int(*fp)(void*vp)){
//查找当前位置后的元素,附加到新列表
//在此处实现函数并更改返回值
返回NULL;
}
int distance fromStart(IteratorgIT){
节点*c=it->curr;
整数计数=0;
如果(c->prev==NULL){
返回0;
}
而(c->prev!=NULL)
{
c=c->prev;
计数++;
返回计数;
}
//计算从当前位置到列表开头的节点数
}
int distanceToEnd(IteratorgIt){
节点*cu=it->curr;
整数计数=0;
如果(cu->next==NULL){
返回0;
}
同时(cu->next!=NULL)
{
cu=cu->next;
计数++;
返回计数;
}
//计算从当前位置到列表末尾的节点数
}
无效重置(IteratorgIT){
it->curr=it->head;
//重置为列表的开始
返回;
}
void freeIt(iteratorgit){
断言(it!=NULL);
节点*curr,*prev;
curr=it->first;
while(curr!=NULL){
上一次=当前;
当前=当前->下一步;
自由(上一个->值);
免费(上);
}
免费(it);
//删除其中的节点并释放内存
}
这是代码的头文件:

#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include "iteratorG.h"

typedef struct Node {

  char   *value;  // value of thee list item (string)
  struct Node *prev;
  // pointer previous node in list
  struct Node *next;
  // pointer to next node in list

  // implemented struct here .. 
} Node;

typedef struct IteratorGRep {

  int  numofit;      // count of items in list
  Node *head;      // first node in list
  Node *curr;       // current node in list
  Node *tail;       // last node in list

  ElmCompareFp  cmpElm;
  ElmNewFp  newElm;
  ElmFreeFp freeElm;

  // implemented struct here .. 

} IteratorGRep;


/*


  // functions below .... 
 */


IteratorG newIterator(ElmCompareFp cmpFp, ElmNewFp newFp, ElmFreeFp freeFp){

    struct IteratorGRep *it;

  it = malloc(sizeof (struct IteratorGRep));
  assert (it != NULL);
  it->numofit = 0;
  it->head = NULL;
  it->tail = NULL;
  it->curr = NULL;
  it->cmpElm=cmpFp;
  it->newElm=newFp;
  it->freeElm=freeFp;
  return it;

    // created list 
}

int  add(IteratorG it, void *vp){

  Node *temp=NULL;
  temp=it->next;
  it->next=vp;
  vp->prev=it;
  vp->next=temp;
  temp->prev=vp;

    // Inserts element pointed by 'vp' into list iterator 'it' 
    return 1;
}
int  hasNext(IteratorG it){

  if(it->next!=NULL)
   {
     return 1;
   }

    // check for any next elements  
    return 0;
}
int  hasPrevious(IteratorG it){

    if(it->prev!=NULL)
    {
      return 1;
    }
    // check for any previous elements 
    return 0;
}
void *next(IteratorG it){
  if(it->next!=NULL)
  {
    it=it->next;
    Node *curre=it->curr;
    return curre;

  }
    // move to next element
    return NULL;
}
void *previous(IteratorG it){

    if(it->prev!=NULL)
    {
      it=it->prev;
      Node *curre=it->curr;
      return curre;

    }

    // moves to previous element
    return NULL;
}
int  del(IteratorG it){
  if(it->prev!=NULL)
  {
    Node *temp_curr=it->curr;
    Node *temp_prev=it->prev->prev;
    temp_curr->prev=temp_prev;
    temp_prev->next=temp_curr;
    return 1;

  }
     // removes previous element from list 
  if(it->prev==NULL)
  {
    return 0;
  }
}
int  set(IteratorG it, void *vp){
  if(it->prev!=NULL)
  {

  Node *store_curr=it->curr;
  Node *store_prev=it->prev->prev;

  store_curr->prev=vp;
  vp->next=store_curr;
  store_prev->next=vp;
  vp->prev=store_prev;
  return 1;
  }
    // Replaces previous element with the element (*vp) 
    return 0;
}
IteratorG advance(IteratorG it, int n){


    // Advance by n times and return list with n times of elements
    // To  implement function here and change return value 
    return NULL;
}
void reverse(IteratorG it){
  Node *curr = it->head;
  Node *temp = NULL;
  while(curr != NULL) {
    temp = curr->next;
    curr->next = curr->prev;
    curr->prev = temp;
    curr = temp;
  }
  temp = it->head;
  it->head = it->tail;
  it->tail = temp;    

    // reverses order of list  
}
IteratorG find(IteratorG it, int (*fp) (void *vp) ){

    // finds elements after current position,append to new list
    // To implement function here and change return value 

     return NULL;
}

int distanceFromStart(IteratorG it){

  Node *c=it->curr;
  int count=0;

  if (c->prev==NULL){
    return 0;  
  }

  while(c->prev!=NULL)
  {
    c=c->prev;
    count++;
    return count;
  }


     // counts number of nodes from current position to start of list 

}
int distanceToEnd(IteratorG it){

  Node *cu=it->curr;
  int count=0;

  if (cu->next==NULL){
    return 0;  
  }
  while(cu->next!=NULL)
  {
    cu=cu->next;
    count++;
    return count;
  }
    // counts number of nodes from current position to end of list  

}
void reset(IteratorG it){

  it->curr=it->head;

    // reset to start of list  
    return;
}
void freeIt(IteratorG it){
  assert(it != NULL);
  Node *curr, *prev;
  curr = it->first;
  while (curr != NULL) {
    prev = curr;
    curr = curr->next;
    free(prev->value);
    free(prev);
  }
  free(it); 

    // remove nodes in it and free memory  

}
#ifndef LISTITERATORG_H
#define LISTITERATORG_H

#include <stdio.h>

typedef struct IteratorGRep *IteratorG;

typedef int   (*ElmCompareFp)(void const *e1, void const *e2);
typedef void *(*ElmNewFp)(void const *e1);
typedef void  (*ElmFreeFp)(void *e1);


IteratorG newIterator(ElmCompareFp cmpFp, ElmNewFp newFp, ElmFreeFp freeFp);
int  add(IteratorG it, void *vp);
int  hasNext(IteratorG it);
int  hasPrevious(IteratorG it);
void *next(IteratorG it);
void *previous(IteratorG it);
int  del(IteratorG it);
int  set(IteratorG it, void *vp);
IteratorG advance(IteratorG it, int n);
void reverse(IteratorG it);
IteratorG find(IteratorG it, int (*fp) (void *vp) );
int distanceFromStart(IteratorG it);
int distanceToEnd(IteratorG it);
void reset(IteratorG it);
void freeIt(IteratorG it);

#endif
\ifndef listiterator\u H
#定义LISTITERATORG_H
#包括
typedef struct IteratorGRep*IteratorG;
typedef int(*elmcarpefp)(无效常数*e1,无效常数*e2);
typedef void*(*ElmNewFp)(void const*e1);
typedef void(*ElmFreeFp)(void*e1);
IteratorG newIterator(ElmCompareFp cmpFp、ElmNewFp newFp、ElmFreeFp freeFp);
int-add(iteratorgit,void*vp);
int hasNext(iteratorgit);
int hasPrevious(迭代组织它);
void*next(迭代组织它);
void*上一个(迭代组织它);
int del(迭代组织it);
int集(iteratorgit,void*vp);
IteratorgAdvance(IteratorgIT,intn);
无效反向(IteratorgIt);
迭代组织查找(iteratorgit,int(*fp)(void*vp));
int distance fromStart(IteratorgIt);
int distanceToEnd(IteratorgIt);
无效重置(IteratorgIt);
void freeIt(迭代组织它);
#恩迪夫

其中一个函数尚未实现,并在代码本身中指出。但我想这可能不是问题的根源。我认为节点在这里没有得到正确使用。

确实,您的
struct IteratorGRep
不包含成员
next
prev
,因此编译器是正确的告诉你真相。而且
void*vp
不是指向
struct
类型的指针,因此您也无法访问
vp->next
vp->prev
!为什么您认为节点中的成员可以被引用为IteratorGRep的成员?显然,迭代器