c typedef(ed) 不透明指针 [英] c typedef(ed) opaque pointer

查看:43
本文介绍了c typedef(ed) 不透明指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经定义了一个不透明的结构和相关的 API,如下所示:

I've defined an opaque structure and related APIs like this:

typedef struct foo foo;
foo *create_foo(...);
delete_foo(foo *f);

我无法在我的 c 文件中定义结构.给出重新定义错误.

I am not able to define the structure in my c file. Gives redefinition error.

typedef struct foo {
   int implementation;
}foo;

我可以在没有 typedef 的情况下在 c 文件中使用 foo 但我想要 typedef(即直接将它用作 foo*).有办法吗?

I am able to use foo in c file without typedef but I want the typedef (i.e. use it directly as foo*). Is there a way?

推荐答案

你的头文件中已经有 typedef,所以在实现中包含它并定义 struct foo没有 typedef.

You already have the typedef in your header, so include that and define struct foo in the implementation without the typedef.

foo.h:

typedef struct foo foo;
foo *create_foo(...);
delete_foo(foo *f);

foo.c:

#include <foo.h>

struct foo { int implementation; };
/* etc. */

这篇关于c typedef(ed) 不透明指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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