2016-02-12 97 views
2

我收到了一个错误error: conflicting types for 'initList',其中有一些代码是通过.c文件和.h文件写入的。下面是.c文件中的代码:C - .h和.c文件中的冲突类型

#include "lists.h" 

void initList(LIST* list) { 
    list->head.coef = 0; 
    list->head.exp = 0; 
} 

下面是在.h文件中相应的代码:

#ifndef LISTS_H__ 
#define LISTS_H__ 

#include <stdio.h> 
#include <string.h> 

typedef struct node { 
    int coef; 
    int exp; 
    struct node* next; 
} NODE; 

typedef struct list { 
    NODE head; 
} LIST; 

void initList(LIST*); 

#endif 

我真不明白我在做什么错在这里...我已经检查了其他类似的问题,但是他们的答案中没有一个提出了解决方案,并且/或者我已经有了修复程序。

+2

您是使用C编译器编译还是使用C++编译器? – paddy

+0

我在Windows上使用MinGW的gcc。 – Ryan

+0

如果将参数名称(列表)添加到声明中,会发生什么情况? –

回答

0

首先,变量的单词列表不是一个好的选择。继续。

从我所看到的情况看来,您正在制作一个自引用结构,尽管头元素是一个单独的结构,但与结点具有相同的结构。

像一些评论,我也没有得到任何错误与您发布的代码,所以为了帮助你,这里是一些代码,使用你必须创建一个列表,然后添加值并链接列表和节点在一起。我还使用 - >运算符更改了最后一个节点。

我希望这可以帮助你。

在h文件中。

/* 
* 35352860_list.h 
*/ 

#ifndef _35352860_LIST_H__ 
#define _35352860_LIST_H__ 

#include <stdio.h> 
#include <string.h> 

typedef struct node 
    { 
     int coef; 
     int exp; 
     struct node* next; 
    } NODE; 

typedef struct list 
    { 
    NODE head; 
    } LIST; 

void initList(LIST*); 

#endif /* _35352860_LIST_H__ */ 

和c文件。

/* 
* 35352860_main.c 
*/ 

#include "35352860_list.h" 

void initList(LIST* list) 
    { 
    list->head.coef = 0x0000; 
    list->head.exp = 0x0000; 
    list->head.next = NULL; 
    } 


int main 
    (
    unsigned int argc, 
    unsigned char *arg[] 
    ) 
{ 
    LIST list; 
    NODE node_1; 
    NODE node_2; 

    printf("There is 1 variable of type LIST and 2 variable of type NODE.\n"); 
    printf("Address of list................................0x%.8X\n", &list); 
    printf("Value of list.head.coef........................0x%.8X\n", list.head.coef); 
    printf("Value of list.head.exp.........................0x%.8X\n", list.head.exp); 
    printf("Value of list.head.next........................0x%.8X\n", list.head.next); 
    printf("The nodes            Node_1  Node_2\n"); 
    printf("Address of nodes.........................................0x%.8X 0x%.8X\n", &node_1, &node_2); 
    printf("Value of nodes.coef......................................0x%.8X 0x%.8X\n", node_1.coef, node_2.coef); 
    printf("Value of nodes.exp.......................................0x%.8X 0x%.8X\n", node_1.exp, node_2.exp); 
    printf("Value of nodes.next......................................0x%.8X 0x%.8X\n", node_1.next, node_2.next); 
    printf("\n\nCall initList.\n"); 
    initList(&list); 
    printf("Address of list.head...........................0x%.8X\n", &list); 
    printf("Value of list.head.coef........................0x%.8X\n", list.head.coef); 
    printf("Value of list.head.exp.........................0x%.8X\n", list.head.exp); 
    printf("Value of list.head.next........................0x%.8X\n", list.head.next); 
    printf("The nodes            Node_1  Node_2\n"); 
    printf("Address of nodes.........................................0x%.8X 0x%.8X\n", &node_1, &node_2); 
    printf("Value of nodes.coef......................................0x%.8X 0x%.8X\n", node_1.coef, node_2.coef); 
    printf("Value of nodes.exp.......................................0x%.8X 0x%.8X\n", node_1.exp, node_2.exp); 
    printf("Value of nodes.next......................................0x%.8X 0x%.8X\n", node_1.next, node_2.next); 
    printf("\n\nLinking node_1 to list, and add some values to node_1.\n"); 
    list.head.next = &node_1; 
    node_1.coef = 0x1111; 
    node_1.exp = 0x1111; 
    node_1.next = NULL; 
    printf("Address of list.head...........................0x%.8X\n", &list); 
    printf("Value of list.head.coef........................0x%.8X\n", list.head.coef); 
    printf("Value of list.head.exp.........................0x%.8X\n", list.head.exp); 
    printf("Value of list.head.next........................0x%.8X\n", list.head.next); 
    printf("The nodes            Node_1  Node_2\n"); 
    printf("Address of nodes.........................................0x%.8X 0x%.8X\n", &node_1, &node_2); 
    printf("Value of nodes.coef......................................0x%.8X 0x%.8X\n", node_1.coef, node_2.coef); 
    printf("Value of nodes.exp.......................................0x%.8X 0x%.8X\n", node_1.exp, node_2.exp); 
    printf("Value of nodes.next......................................0x%.8X 0x%.8X\n", node_1.next, node_2.next); 
    printf("\n\nLinking node_2 to node_1, and add some values to node_2.\n"); 
    node_1.next = &node_2; 
    node_2.coef = 0x2222; 
    node_2.exp = 0x2222; 
    node_2.next = NULL; 
    printf("Address of list.head...........................0x%.8X\n", &list); 
    printf("Value of list.head.coef........................0x%.8X\n", list.head.coef); 
    printf("Value of list.head.exp.........................0x%.8X\n", list.head.exp); 
    printf("Value of list.head.next........................0x%.8X\n", list.head.next); 
    printf("The nodes            Node_1  Node_2\n"); 
    printf("Address of nodes.........................................0x%.8X 0x%.8X\n", &node_1, &node_2); 
    printf("Value of nodes.coef......................................0x%.8X 0x%.8X\n", node_1.coef, node_2.coef); 
    printf("Value of nodes.exp.......................................0x%.8X 0x%.8X\n", node_1.exp, node_2.exp); 
    printf("Value of nodes.next......................................0x%.8X 0x%.8X\n", node_1.next, node_2.next); 
    printf("\n\nChange the value node_2.coef using the -> operator.\n"); 
    list.head.next -> next -> coef = 0x3333; 
    printf("Address of list.head...........................0x%.8X\n", &list); 
    printf("Value of list.head.coef........................0x%.8X\n", list.head.coef); 
    printf("Value of list.head.exp.........................0x%.8X\n", list.head.exp); 
    printf("Value of list.head.next........................0x%.8X\n", list.head.next); 
    printf("The nodes            Node_1  Node_2\n"); 
    printf("Address of nodes.........................................0x%.8X 0x%.8X\n", &node_1, &node_2); 
    printf("Value of nodes.coef......................................0x%.8X 0x%.8X\n", node_1.coef, node_2.coef); 
    printf("Value of nodes.exp.......................................0x%.8X 0x%.8X\n", node_1.exp, node_2.exp); 
    printf("Value of nodes.next......................................0x%.8X 0x%.8X\n", node_1.next, node_2.next); 

    return(0x0000); 
}