访问结构:derefrencing指向不完全类型 [英] accessing struct: derefrencing pointer to incomplete type

查看:504
本文介绍了访问结构:derefrencing指向不完全类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我试图使用和访问指向我的结构我不断收到提领指向不完全类型恼人的消息....

When I'm trying to use and access the pointers to my structs i keep getting the annoying message of "dereferencing pointer to incomplete type" ....

例如在我的 user.h 文件我有这样的的typedef

For example in my user.h file I have this typedef:

typedef struct FacebookUser_t* User;

在我的 user.c的文件,其中包括 user.h 我有这样的结构:

and in my user.c file which includes user.h I have this struct:

struct FacebookUser_t {...}; 



所以,当我需要一个指向结构我只用用户布拉布拉;
它似乎工作,我把它添加到泛型列表为元素无效* ,这就是类型定义为它在 list.h


So when I need a pointer to that struct I only use User blabla; it seems to work, and I add it to generic list as Element which is void* and that's the typedef for it in list.h:

typedef void* Element;

和我回来的时候从包含列表中的节点元素用户)我无法访问它的成员,我做错了什么?谢谢!

and when I get back a node from the list which contains Element (User) I can't access it's members, what I'm doing wrong? Thanks!

推荐答案

如果要隐藏结构的定义(通过在坚持实际结构{块单个C文件,只在头露出的typedef 主编的名字,你不能指望直接访问的字段。

If you want to hide the definition of a structure (by sticking the actual struct { block in a single C file and only exposing a typedefed name in the header, you cannot expect to access the fields directly.

解决此问题的办法是继续与封装,并定义访问功能,即你必须(在 user.h

One way around this is to continue with the encapsulation, and define accessor functions, i.e. you'd have (in user.h):

const char * user_get_name(const User user);
void         user_set_name(User user, const char *new_name);
...

请注意,包括 * 的typedef 往往是混乱的,在我看来。

Please note that including the * in the typedef is often confusing, in my opinion.

这篇关于访问结构:derefrencing指向不完全类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆