编译错误:请求成员在某个非结构或联合中。

时间:2022-01-14 21:21:10

Edit: The code below has been modified to work as the problem has been solved.

编辑:下面的代码已经被修改了,因为问题已经解决了。

Specifically, (*hardwareList.next_item)->next was originally written without brackets (e.g. as *hardwareList.next_item->next) and the compiler didn't understand it.

具体来说,(*hardwareList.next_item)->最初是没有括号的(例如,如*hardwareList.next_item->),而编译器不理解它。

I'm trying to workout why the compiler is getting confused with my C code. I'm trying to create a linked list that stores all the items and also a pointer to the address-of the last "next" variable, for easy appending.

我试着去锻炼为什么编译器会和我的C代码混淆。我正在尝试创建一个链接的列表,该列表存储所有的项目,并将一个指针指向最后一个“下一个”变量的地址,以方便添加。

typedef struct {
  int recordNum;
  char toolName[25];
  int quantity;
  float cost;
} HardwareData;

typedef struct _HardwareListItem{
  HardwareData data;
  struct _HardwareListItem* next;
} HardwareListItem;

typedef struct _HardwareList {
  HardwareListItem* items;
  HardwareListItem** next_item;
} HardwareList;

HardwareList readFromFile(FILE* fp)
{
  char stopReading = 0;
  HardwareList hardwareList = {0};
  hardwareList.next_item = &hardwareList.items;
  do {
    *hardwareList.next_item = (HardwareListItem*)calloc(1, sizeof(HardwareData));
    if (*hardwareList.next_item == NULL)
    {
      fprintf(stderr, "OOM Reading File\n");
      fflush(stderr);
      exit(EXIT_FAILURE);
    }
    if (fread(&((*hardwareList.next_item)->data), sizeof(HardwareData), 1, fp) != 1) {
      free(*hardwareList.next_item);
      *hardwareList.next_item = NULL;
      stopReading = 1;
    } else {
      hardwareList.next_item = &((*hardwareList.next_item)->next);
    }
  } while(!stopReading);

  return hardwareList;
}

Compiler says:

编译器说:

line 31: error: request for member 'data' in something not a structure or union
line 36: error: request for member 'next' in something not a structure or union

2 个解决方案

#1


7  

My guess the problem is this piece of code: *(hardwareList.next_item)->data

我猜问题是这段代码:*(hardwareList.next_item)->数据。

next_item is a pointer to a pointer, so my guess is that the compiler reads this as *((hardwareList.next_item)->data) which of course doesn't work - pointers don't have any members in C.

next_item是一个指向指针的指针,所以我的猜测是编译器将它读为*((hardwareList.next_item)->数据),当然它不工作——指针在C中没有任何成员。

Try ((*(hardwareList.next_item))->data) to get the correct dereference order.

尝试(*(hardwareList.next_item))->数据)以获得正确的取消引用顺序。

#2


4  

hardwareList.next_item is HardwareListItem**, so operator -> on it returns HardwareListItem*, which obviously is not a struct.

hardwareList。next_item是HardwareListItem**,所以操作符->会返回HardwareListItem*,这显然不是一个struct。

You're using too many pointers, it is confusing. Try to simplify your code, you have tons of bugs there.

你使用了太多的指针,这很让人困惑。试着简化你的代码,那里有大量的bug。

#1


7  

My guess the problem is this piece of code: *(hardwareList.next_item)->data

我猜问题是这段代码:*(hardwareList.next_item)->数据。

next_item is a pointer to a pointer, so my guess is that the compiler reads this as *((hardwareList.next_item)->data) which of course doesn't work - pointers don't have any members in C.

next_item是一个指向指针的指针,所以我的猜测是编译器将它读为*((hardwareList.next_item)->数据),当然它不工作——指针在C中没有任何成员。

Try ((*(hardwareList.next_item))->data) to get the correct dereference order.

尝试(*(hardwareList.next_item))->数据)以获得正确的取消引用顺序。

#2


4  

hardwareList.next_item is HardwareListItem**, so operator -> on it returns HardwareListItem*, which obviously is not a struct.

hardwareList。next_item是HardwareListItem**,所以操作符->会返回HardwareListItem*,这显然不是一个struct。

You're using too many pointers, it is confusing. Try to simplify your code, you have tons of bugs there.

你使用了太多的指针,这很让人困惑。试着简化你的代码,那里有大量的bug。