我有以下主.c文件:

#include <stdio.h>
#include <stdlib.h>
#include <wctype.h>

#include "lista.h"

int main(int argc, char *argv[])
{
    struct nod *root = NULL;
    root = init(root);

    return 0;
}

还有lista.h:
#ifndef LISTA_H_INCLUDED
#define LISTA_H_INCLUDED

#include "lista.c"

typedef struct nod
{
    int Value;
    struct nod *Next;
}nod;

nod* init(nod *);
void printList(nod *);

#endif // LISTA_H_INCLUDED

最后是lista.c,它是:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

#include "lista.h"

nod* init(nod *root)
{
    root = NULL;
    return root;
}

void printList(nod *root)
{
    //We don't want to change original root node!
    nod *aux = root;

    printf("\n=== Printed list =====\n");
    while (aux != NULL)
    {
        printf(aux->Value);
        aux = aux->Next;
    }
    puts("\n");
}

即使在包含了头文件之后,我也会得到三个错误,分别是:
未知类型名“nod”
如何使lista.h中的typedef显示在lista.c中?
我就是搞不清这里发生了什么。

最佳答案

查看lista.h头文件:

#ifndef LISTA_H_INCLUDED
#define LISTA_H_INCLUDED

#include "lista.c"

[..]

#endif // LISTA_H_INCLUDED

你包括lista.c,你根本不应该这么做。出现错误,因为那时还没有定义nod

关于c - 从头到源传递typedef-C,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31327450/

10-11 15:53