1

我试图写一个简单的链表只是为了练习和一点点记忆,但我遇到了麻烦。我在 C 方面非常缺乏经验,我不明白为什么这不起作用。我正在尝试定义一个 Node 和一个 LinkedList 结构,但每次我尝试编译时都会收到一条错误消息,指出 Node 是未知类型。我确定我错过了一些东西,但我无法弄清楚。谢谢大家!

这是我的 ll.h 文件

  1 #ifndef ll_h
  2 #define ll_h
  3 
  4 #include <stdio.h>
  5 
  6 typedef struct {
  7     void *data;
  8     Node *next;
  9     Node *prev;
 10 } Node;
 11 
 12 
 13 typedef struct {
 14     Node *first;
 15     Node *last;
 16     int size;
 17 } LinkedList;
 18 
 19 
 20 void *getData(LinkedList list, int index);
 21 int getSize(LinkedList list);
 22 void *deleteNode(LinkedList, int index);
 23 void add(LinkedList list, void *data);
 24 void freeList(LinkedList list);
 25 
 26 #endif           

我收到的错误

cc -Wall -g   -c -o ll.o ll.c
In file included from ll.c:3:0:
ll.h:8:5: error: unknown type name ‘Node’
ll.h:9:5: error: unknown type name ‘Node’
ll.c: In function ‘getData’:
ll.c:8:18: error: expected expression before ‘LinkedList’
ll.c:12:7: warning: assignment from incompatible pointer type [enabled by default]
ll.c: In function ‘getSize’:
ll.c:21:12: error: expected expression before ‘LinkedList’
ll.c: In function ‘deleteNode’:
ll.c:26:18: error: expected expression before ‘LinkedList’
ll.c:32:7: warning: assignment from incompatible pointer type [enabled by default]
ll.c:37:12: error: request for member ‘next’ in something not a structure or union
ll.c:38:12: warning: assignment from incompatible pointer type [enabled by default]
ll.c:40:12: error: request for member ‘next’ in something not a structure or union
ll.c: In function ‘add’:
ll.c:52:16: warning: assignment from incompatible pointer type [enabled by default]
ll.c:54:21: warning: assignment from incompatible pointer type [enabled by default]
ll.c: In function ‘freeList’:
ll.c:61:18: error: expected expression before ‘LinkedList’
ll.c:62:18: warning: initialization from incompatible pointer type [enabled by default]
ll.c:65:7: warning: assignment from incompatible pointer type [enabled by default]
ll.c:67:7: warning: assignment from incompatible pointer type [enabled by default]
ll.c:62:11: warning: variable ‘next’ set but not used [-Wunused-but-set-variable]
ll.c:60:9: warning: unused variable ‘i’ [-Wunused-variable]
ll.c: In function ‘main’:
ll.c:85:6: error: ‘LinkedList’ has no member named ‘add’
ll.c:91:10: warning: dereferencing ‘void *’ pointer [enabled by default]
ll.c:91:10: error: void value not ignored as it ought to be
ll.c: In function ‘getSize’:
ll.c:22:1: warning: control reaches end of non-void function [-Wreturn-type]
make: *** [ll.o] Error 1
4

3 回答 3

3

用这个:

typedef struct Node{
     void *data;
     struct Node *next; // note here
     struct Node *prev;
} Node;
于 2013-08-31T05:09:25.620 回答
2

您要么需要前向声明,要么需要删除 typedef。您还可以将 typedef 与两次节点一起使用。

前向声明

typedef struct Node Node; // Necessary in C, harmless (but non-idiomatic) in C++

typedef struct {
    void *data;
    Node *next;
    Node *prev;
} Node;

没有 typedef(仅适用于 C++,不适用于 C)

struct Node {
    void *data;
    Node *next;
    Node *prev;
};

使用带有标签的 typedef

适用于 C 和 C++,但不适用于 C++。

typedef struct Node {
    void *data;
    struct Node *next;
    struct Node *prev;
} Node;
于 2013-08-31T05:13:44.203 回答
0

为谷歌搜索带来的下一位读者发布答案:
error: void value not ignored as it ought to be

请记住,在尝试实现伪代码时,函数指针会使用函数名称进行初始化,而无需任何括号。菜鸟失误?也许吧,但我并不完全是菜鸟(90 年代中期在大学教授 C/C++ 实验室),这让我受益匪浅。如果我输入它而不是剪切粘贴,我可能会发现错误,因为我知道不要创建该代码......

为完整性提供声明的示例:

//  Static menu manager
#define MAX_SELECTIONS 4
#define MAX_PROMPT_LEN 20
typedef struct _selection {
    char prompt[MAX_PROMPT_LEN];
    void(*function) ( void );
    int fn_arg;
} SELECTION ;

typedef struct _menu {
    int id;
    int num_selections;
    SELECTION selection[MAX_SELECTIONS];
} MENU;

enum _menu_ids { MAIN_MENU, DEPOSIT_MENU, WITHDRAWL_MENU, BALANCE_MENU };
enum _menu_keys { DEPOSIT, WITHDRAWL, BALANCE, CHECKING, SAVINGS };



错误的:

//  WRONG
static MENU menu[] = {
    { MAIN_MENU, 4,
        {"Perform A Deposit", goto_menu(), DEPOSIT_MENU},
        {"Perform a Withdrawl", goto_menu(), WITHDRAWL_MENU },
        {"Get Balance", goto_menu(), BALANCE_MENU },
        {"Hide Menu", menu_hide(), 0}
    }
}



正确的:

//  Correct
static MENU menu[] = {
    { MAIN_MENU, 4, {
        {"Perform A Deposit", goto_menu, DEPOSIT_MENU},
        {"Perform a Withdrawl", goto_menu, WITHDRAWL_MENU },
        {"Get Balance", goto_menu, BALANCE_MENU },
        {"Hide Menu", menu_hide, 0} }
    }
}

特别细心的读者可能会注意到伪代码中缺少初始化 's 成员struct所需的额外大括号。arraygcc 编译器很友好地提供了一个有用的错误:
MENU: improper number of initializers.

于 2013-09-11T06:14:54.627 回答