Ошибка «неизвестное имя типа» в C с Typedef в .h файле - PullRequest
0 голосов
/ 07 марта 2020

Я пытаюсь создать связанный список в C с использованием структур узлов, но у меня возникает ошибка, при которой мои функции получают «ошибку неизвестного типа».

    #ifndef Node_h
    #define Node_h

    #include <stdio.h>
    /* Add employee struct and Node struct */

    typedef struct Employee {
        char *firstName;
        char *lastName;
    }employee_t;

    typedef struct Node{
        struct employee_t *empInfo;
        struct node_t *next;
    }node_t;
    /*
     Prints all employee names in the following format
     LastName, firstName
    */
    void print_list (node_t * head);

    /*
    Adds a new node to the end of the list
    */
    void addToEnd(node_t **head, employee_t *employee);  

    /*
    ...

Я определил свои структуры и все мои функции в этом файле Node.h. Ниже в моем узле. c файл, где находятся мои функции, где происходят ошибки.

    #include <stdio.h>
    /* Add employee struct and Node struct */

    /*
     Prints all employee names in the following format
     LastName, firstName
    */

    void print_list (node_t * head){ //here is where the error occurs
        node_t *current_node;
        current_node = head;
        while(current_node != NULL){
            printf("%s", current_node->empInfo);
            current_node = current_node->next;
        }
    }
    /*
     Adds a new node to the end of the list
     */
     void addToEnd(node_t **head, employee_t *employee){ //here is where the error occurs
        node_t *current_node;
        node_t *new_node;
        current_node = *head;
        while(current_node->next != NULL){
            current_node = current_node->next;
        }
        new_node = (node_t *) malloc(sizeof(node_t));
        current_node->next = new_node;
        new_node->empInfo = employee;
        new_node->next = NULL;
    } 

    ...

в моих объявлениях функций в узле. c Я получаю ошибку "неизвестный тип ошибки" node_t ', вы имели в виду ... "или" неизвестный тип ошибки ", неизвестный тип ошибки" employee_t ", вы имели в виду ..."

Вот мой основной для дополнительного контекста:

     #include <stdio.h>
     #include <stdlib.h>
     #include "node.h"

     int main (void) {

        setvbuf(stdout, NULL, _IONBF, 0);
        setvbuf(stderr, NULL, _IONBF, 0);

         employee_t *empPtr1, emp1,*empPtr2, emp2,*empPtr3, emp3,*empPtr4, emp4,*empPtr5, 
         emp5,*empPtr6, emp6, *empPtr7, emp7, *empPtr8, emp8, *empPtr9, emp9;
         empPtr1 = &emp1;
         empPtr2 = &emp2;
         empPtr3 = &emp3;
         empPtr4 = &emp4;
         empPtr5 = &emp5;
         empPtr6 = &emp6;
         empPtr7 = &emp7;
         empPtr8 = &emp8;
         empPtr9 = &emp9;
         node_t head;
         node_t *headPtr;
         node_t **headPtr2;
         headPtr2 = &headPtr;
         headPtr = &head;

             printf("Please enter the first name of the first employee you'd like to add:");
             scanf("%s", empPtr1->firstName);
             printf("Please enter the last name of the first employee you'd like to add:");
             scanf("%s", empPtr1->lastName);

         addToEnd(headPtr2, empPtr1);
    ...

Буду признателен за любую помощь, которую не смогу получить, спасибо!

1 Ответ

2 голосов
/ 07 марта 2020

Сначала необходимо включить файл заголовка в исходный файл '*. c': #include "node.h"

Вы также смешиваете пространства имен typedef и struct внутри файла заголовка:

typedef struct Employee {
    char *firstName;
    char *lastName;
}employee_t;

typedef struct Node{
    struct employee_t *empInfo; // this should be either 'employee_t *' or 'struct Employee *'
    struct node_t *next; // This should be 'struct Node *'
}node_t;
...