我正在尝试构建一个链表,但在编译我的代码时出现以下错误:
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;
此外,欢迎提供一般提示!请批评我的代码,但您觉得有必要 - 如果没有人指出错误,我将永远不会从错误中吸取教训 =) 谢谢!!!
最佳答案
您必须在尝试使用它之前声明该类型。
因此,如果您将结构定义/typedef 移到函数原型(prototype)之前,编译器应该停止提示隐式/冲突定义。
关于c++ - 编译器提示类型冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24966642/