【问题标题】:Passing a typedef from header to source - C将 typedef 从标头传递到源 - C
【发布时间】:2015-07-09 20:20:19
【问题描述】:

我有以下 ma​​in.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");
}

即使在包含头文件之后,我也会收到三个错误: 未知类型名称“点头”

如何使 lista.h 中的 typedef 可以在 lista.c 上看到?

我就是想不通这里发生了什么。

【问题讨论】:

  • lista.h 中删除#include "lista.c"。在实际定义nod 之前,它会引入所有lista.c 内容(引用nod)。通常,.h 文件从不包含.c。反之亦然。
  • 原则上从不包含 C 文件。既不在标题中,也不在来源中。

标签: c data-structures typedef ansi-c


【解决方案1】:

看看你的 lista.h 头文件:

#ifndef LISTA_H_INCLUDED
#define LISTA_H_INCLUDED

#include "lista.c"

[..]

#endif // LISTA_H_INCLUDED

您包含 lista.c,您根本不应该这样做。并发生错误,因为当时nod 尚未定义。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-23
    • 2020-07-09
    • 2021-12-24
    • 2021-11-27
    • 1970-01-01
    • 1970-01-01
    • 2018-04-04
    • 1970-01-01
    相关资源
    最近更新 更多