我在C上建立了一个动态列表,该列表应该保存输入的单词并将其写到txt文件中。但是,我不断收到此错误:
lista.h:4:14: error: unknown type name ‘Node’; did you mean ‘mode_t’?
int is_empty(Node *head){
^~~~
mode_t
lista.h:11:19: error: unknown type name ‘Node’; did you mean ‘mode_t’?
void insert_first(Node **head_ref, Node *element){
^~~~
mode_t
lista.h:11:36: error: unknown type name ‘Node’; did you mean ‘mode_t’?
void insert_first(Node **head_ref, Node *element){
^~~~
mode_t
lista.h:16:17: error: unknown type name ‘Node’; did you mean ‘mode_t’?
void print_list(Node *head){
^~~~
mode_t
lista.h:29:18: error: unknown type name ‘Node’; did you mean ‘mode_t’?
void insert_last(Node **head, Node *element){
^~~~
mode_t
lista.h:29:31: error: unknown type name ‘Node’; did you mean ‘mode_t’?
void insert_last(Node **head, Node *element){
^~~~
mode_t
lista.h:43:20: error: unknown type name ‘Node’; did you mean ‘mode_t’?
void write_in_file(Node *head){
^~~~
mode_t
这很奇怪,因为我使用typedef声明了Node:
#include <stdio.h>
#include <stdlib.h>
int is_empty(Node *head){
if(head == NULL)
return 1;
else
return 0;
}
void insert_first(Node **head_ref, Node *element){
element->next = *head_ref;
*head_ref = element;
}
void print_list(Node *head){
if(is_empty(head))
printf("Lista vuota\n");
else{
Node *tmp = head;
while(tmp != NULL){
printf("%s\n", tmp->parola);
tmp = tmp->next;
}
}
}
void insert_last(Node **head, Node *element){
Node *curr = *head, *prev = NULL;
while(curr != NULL){
prev = curr;
curr = curr->next;
}
if(prev == NULL)
*head = element;
else{
prev->next = element;
element = curr;
}
}
void write_in_file(Node *head){
Node *iterator = head;
Node *tmp = head;
FILE *fptr;
if((fptr = fopen("newfile.txt", "w+")) == NULL){
printf("Impossibile creare il file\n");
} else {
while(iterator != NULL){
fprintf(fptr, "%p %s\n", &iterato->next, iterator->parola);
iterator = iterator->next;
}
}
}
struct list_node{
struct list_node *next;
char parola[500];
};
typedef struct list_node Node;
void print_usage();
int is_empty(Node *);
void print_list(Node *);
void insert_first( Node **, Node *);
void insert_last(Node **, Node *);
void write_in_file(Node *);
这是主要内容:
#include <stdio.h>
#include <stdlib.h>
#include "lista.h"
void print_usage(){
printf("\n 1 per aggiungere una parola in fondo"
"\n 2 per aggiungere in testa"
"\n 3 per stampare tutto"
"\n 4 per stampare quella con più vocali"
"\n 5 per scrivere su file"
"\n 6 per uscire\n");
}
int main(){
Node *head = NULL;
Node *item = NULL;
int choice = 0;
while(1){
print_usage();
scanf("%d", &choice);
switch(choice){
case 1:
item = malloc(sizeof(Node));
if(item == NULL) {
printf("Errore nella malloc");
} else {
printf("Inserisci la parola da aggiungere\n");
scanf("%s", item->parola);
insert_last(&head, item);
puts("\n");
}
break;
case 2:
item = malloc(sizeof(Node));
if(item == NULL) {
printf("Errore nella malloc");
} else {
printf("Inserisci la parola da aggiungere");
scanf("%s", item->parola);
insert_first(&head, item);
puts("\n");
}
break;
case 3:
print_list(head);
break;
case 4:
break;
case 5:
write_in_file(head);
break;
case 6:
printf("Exit\n");
exit(EXIT_SUCCESS);
}
}
}
我找不到错误,我的想法是它在函数的声明中,看起来Node对于编译器是完全未知的,尽管我使用typedef声明它指向数据结构
答案 0 :(得分:1)
您将收到错误error: unknown type name ‘Node’
,因为编译Node
文件的内容时,编译器找不到list.h
的定义。
这意味着,当编译器遇到此问题
int is_empty(Node *head){
到此为止,Node
对于编译器来说是未知的,因为它在下面的代码中定义。
请注意,仅在使用typedef
之前移动Node
并不能解决您的问题,因为它只是struct list_node
的前向声明。编译器在使用代码之前也应该知道结构的成员。因此,在将struct list_node
定义和typedef
Node
在文件中使用之前,应同时移动它们。同样,将函数定义写入头文件也不是一个好习惯。只需将原型和结构定义保留在头文件中,然后将功能定义移至.c
文件中即可。
答案 1 :(得分:0)
它不是C#,并且您需要在代码中第一次出现的位置之前声明和定义。
Node node;
typedef int Node;
是错误的,但
typedef int Node;
Node node;
是正确的