gpt4 book ai didi

c - 错误消息显示为 'struct IteratorG' 没有名为 next 的成员'

转载 作者:行者123 更新时间:2023-11-30 19:05:25 26 4
gpt4 key购买 nike

嗨,我正在尝试实现一个可以存储任何类型元素的通用列表迭代器。它还有其他文件来处理正整数类型和字符串类型。但是,我收到一条错误,指出 struct IteratorGRep 没有名为 的成员'next'。我使用的是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

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

最佳答案

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;

没有声明名为“next”的成员。我认为您想访问节点的“下一个”变量,例如

it->curr->next

关于c - 错误消息显示为 'struct IteratorG' 没有名为 next 的成员',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49845154/

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