我正在尝试构建链接列表,但在编译代码时出现以下错误:
In file included from linkedlist.c:1:
linkedlist.h:9: warning: 'struct list_t' declared inside parameter list
linkedlist.h:9: warning: its scope is only this definition or declaration, which is probably not what you want
linkedlist.h:12: warning: 'struct list_t' declared inside parameter list
linkedlist.c: In function 'main':
linkedlist.c:11: warning: passing argument 2 of 'execute_choice' from incompatible pointer type
linkedlist.c: At top level:
linkedlist.c:28: error: conflicting types for 'execute_choice'
linkedlist.h:9: error: previous declaration of 'execute_choice' was here
linkedlist.c:56: error: conflicting types for 'create_node_in_linked_list'
linkedlist.h:12: error: previous declaration of 'create_node_in_linked_list' was here
linkedlist.h:9: warning: 'struct list_t' declared inside parameter list
linkedlist.h:9: warning: its scope is only this definition or declaration, which is probably not what you want
linkedlist.h:12: warning: 'struct list_t' declared inside parameter list
make: *** [all] Error 1
但是,我检查了我的源文件和头文件,'execute_choice'函数在两者中都有匹配的声明。这是我的源文件和头文件:
源文件:
#include "linkedlist.h"
int main (void)
{
int choice;
list_t* list;
list = (list_t*)sizeof(list_t);
while (1) {
choice = display_menu_choices();
execute_choice(choice, list);
}
}
int display_menu_choices(void)
{
int menu_choice;
printf ("Please select from the following options\n");
printf ("0. Exit program\n");
printf ("1. Generate a linkedlist\n");
printf ("2. Sort a linkedlist\n");
scanf ("%d", &menu_choice);
return menu_choice;
}
void execute_choice(int menu_choice, list_t* list)
{
switch (menu_choice) {
case GEN_LL:
generate_linked_list();
break;
case SORT_LL:
sort_linked_list();
break;
case EXIT:
exit(0);
}
return;
}
void generate_linked_list(void)
{
return;
}
void sort_linked_list(void)
{
return;
}
void create_node_in_linked_list(list_t* list)
{
return;
}
标题
#include <stdlib.h>
#include <stdio.h>
#define EXIT 0
#define GEN_LL 1
#define SORT_LL 2
int display_menu_choices(void);
void execute_choice(int, struct list_t*);
void generate_linked_list(void);
void sort_linked_list(void);
void create_node_in_linked_list(struct list_t*);
typedef struct node_t{
struct node_t* current;
struct node_t* next;
int value;
} node_t;
typedef struct list_t{
struct node_t* start;
} list_t;
此外,欢迎提示!请批评我的代码但是你觉得有必要 - 如果没有人指出他们,我永远不会从错误中吸取教训=)谢谢!!!
答案 0 :(得分:3)
在尝试使用之前,您必须声明类型。
因此,如果在函数原型之前移动struct definitions / typedef,编译器应该停止抱怨隐式/冲突定义。
答案 1 :(得分:2)
头文件中的内容顺序错误。 typedef
应出现在函数定义之前。所以移动
typedef struct node_t{
struct node_t* current;
struct node_t* next;
int value;
} node_t;
typedef struct list_t{
struct node_t* start;
} list_t;
之前
int display_menu_choices(void);
void execute_choice(int, struct list_t*);