【发布时间】:2018-10-18 10:33:47
【问题描述】:
我目前正在创建一个 ListNode 结构体,其中包含指向链表中下一个 ListNode 的指针,以及一个指向存储节点信息的 Info 结构体的指针。
我目前有以下代码:
info.h
#ifndef info
#define info
//Define the node structure
typedef struct Info {
size_t pos;
size_t size;
} Info_t;
#endif
listNode.h
#ifndef listNode
#define listNode
//Define the node structure
typedef struct ListNode {
struct Info_t *info;
struct ListNode *next;
} ListNode_t;
ListNode_t* newListNode(ListNode_t* next, Info_t* info);
void destroyListNode(ListNode_t* node);
#endif
listNode.c
#include <stdio.h>
#include <stdlib.h>
#include "info.h"
#include "listNode.h"
ListNode_t* newListNode(ListNode_t* next, Info_t* info)
{
//Set the current node to the head of the linked list
ListNode_t *current = next;
//Move to the next node as long as there is one. We will eventually get to the end of the list
while (current->next != NULL) {
current = current->next;
}
//Create a new node and initialise the values
current->next = malloc(sizeof(ListNode_t));
current->next->info = info;
return current;
}
void destroyListNode(ListNode_t* node)
{
}
当我尝试编译它时,我得到了以下错误,并且我一生都无法弄清楚哪里出了问题。
gcc -g -Wall listNode.c -o listNode
In file included from listNode.c:7:0:
listNode.h:9:24: error: expected identifier or ‘(’ before ‘;’ token
struct Info_t *info;
^
listNode.c: In function ‘newListNode’:
listNode.c:9:1: error: parameter name omitted
ListNode_t* newListNode(ListNode_t* next, Info_t* info)
^~~~~~~~~~
listNode.c:21:25: error: expected identifier before ‘=’ token
current->next->info = info;
^
任何帮助将不胜感激。
【问题讨论】:
-
除了答案,没有
struct Info_t。你有struct Info和它的别名Into_t。 -
您也没有在
listNode.h中定义Info_t,这在技术上不是错误,而是一种非常糟糕的风格,因为它要求用户以特定顺序#include标头。所以如果有人做了#include "listNode.h"并且在下一行#include "info.h",它不会编译。一个头文件应该是自包含的,如果需要的话#include其他头文件。
标签: c linked-list