【问题标题】:C Compile ErrorsC 编译错误
【发布时间】: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


【解决方案1】:

包含保护的名称与您在程序中使用的标识符冲突。你在这里定义info

#define info

具体来说,您将其定义为无。所以这里

current->next->info = info;

变成这样:

current->next->= ;

然后struct Info_t *info; 变成struct Info_t *;。那些显然不会编译。您需要将包含守卫中的info 重命名为不会与其他任何内容冲突的名称,例如INFO_H_GUARD

【讨论】:

    猜你喜欢
    • 2015-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-01
    • 2011-05-16
    相关资源
    最近更新 更多