编译器报错类型冲突

Compiler complains of conflicting types

本文关键字:冲突 类型 编译器      更新时间:2023-10-16

我试图建立一个链表,但在编译我的代码时,我得到以下错误:

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 optionsn");
    printf ("0. Exit programn");
    printf ("1. Generate a linkedlistn");
    printf ("2. Sort a linkedlistn");
    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应该出现在函数定义之前。移动

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*);