找不到typedef的定义

时间:2015-05-12 19:41:20

标签: c typedef

我有file.h

#ifnotdef _FILE_H_
#define _FILE_H_
#include "lista2.h"
    struct texto{
        int col_cursor;
        int lin_cursor;
        lista2* texto;
        int tecla;
    };
    typedef struct texto Texto;  
#endif //_FILE_H_

我有file.c

#include "file.h"
#include "lista2.h"   

     void recua_linha(Texto* t){
           if(!(t->texto->corrente == t->texto->primeiro))
             t->lin_cursor--;
    }   

lista2.h

#ifndef _LISTA2_H_
#define _LISTA2_H_
    typedef struct lista2 lista2;
    typedef struct elemento elemento;
#endif //_LISTA2_H_

lista2.c

#include "lista2.h"
    struct elemento{
        dado_t str;
        elemento* ant;
        elemento* prox;
    };

struct lista2{
    elemento* primeiro;
    elemento* ultimo;
    elemento* corrente;
};

但是当我尝试访问Texto的任何成员时,我得到了

Dereferencing pointer to incomplete type

我知道这意味着:程序知道类型,但无法看到它的实现。我只是不知道为什么或如何解决它。

Ps:我还需要访问Texto文件中的main.c。成员。

1 个答案:

答案 0 :(得分:1)

从file.c获取这段代码:

t->texto->corrente

tTexto结构。现在,从file.h开始,我们有了这个:

lista2* texto;

Texto struct的成员。现在我们来看看file.c对lista2的了解。我们必须查看file.c中包含的头文件,即file.h和lista2.h。第一个没有任何相关的东西。然而,第二个确实有这个:typedef struct lista2 lista2;,这有帮助。 但是,您正在请求名为corrente的数据成员,但lista2.h不提供有关lista2的数据成员的任何信息,因此您应该收到类似于这样:

  

错误:取消引用指向不完整类型的指针

因为所有file.c都知道它在lista2.h中看到的typedef

为了做你想做的事,你应该修改你的lista2.h:

#ifndef _LISTA2_H_
#define _LISTA2_H_
// moved it here, in order to use just 'elemento'
typedef struct elemento elemento;
struct elemento {
  // I do not know what dato_t is...discarded
  elemento* ant;
  elemento* prox;
};

struct lista2 {
  elemento* primeiro;
  elemento* ultimo;
  elemento* corrente;
};

typedef struct lista2 lista2;
#endif //_LISTA2_H_

将lista2.c留空。另请注意,我不明白为什么在结构之前添加选项卡(缩进从文件的第一列开始),所以我将其删除。

顺便说一下,在file.h中,也许你想改变这个

#ifnotdef _FILE_H_

到这个

#ifndef _FILE_H_

因为你应该收到这个

  

无效的预处理程序指令:ifnotdef

请注意,要从Texto访问main(),您应该在主文件中包含file.h。

提示:一个非常好的习惯是使用社区可以(完全)理解您的代码的语言(英语!),如果您需要在将来开发代码,可能现实世界的应用,必须使用英语。

相关问题