#include <stdio.h>
#ifndef API
#define API
struct trytag;
typedef struct trytag try;
void trial (try *);
#endif
Run Code Online (Sandbox Code Playgroud)
#ifndef CORE
#define CORE
struct trytag
{
int a;
int b;
};
#endif
Run Code Online (Sandbox Code Playgroud)
#include "api.h"
#include "core.h"
void trial (try *tryvar)
{
tryvar->a = 1;
tryvar->b = 2;
}
Run Code Online (Sandbox Code Playgroud)
#include "api.h"
int main ()
{
try s_tryvar;
trial(&s_tryvar);
printf("a = %d\nb = %d\n", s_tryvar.a, s_tryvar.b);
}
Run Code Online (Sandbox Code Playgroud)
当我编译时,我得到:
main.c:5: error: storage size of ‘s_tryvar’ isn’t known
Run Code Online (Sandbox Code Playgroud)
如果我core.h在main.c此错误中包含,则不会尝试定义core.h.但我希望try隐藏结构main.c- 它不应该知道try结构的成员.我错过了什么?
我不认为你想做的事情是可能的.编译器需要知道编译的try结构有多大main.c.如果你真的希望它是不透明的,那么创建一个通用指针类型,而不是直接声明变量main(),make alloc_try()和free_try()函数来处理创建和删除.
像这样的东西:
api.h:
#ifndef API
#define API
struct trytag;
typedef struct trytag try;
try *alloc_try(void);
void free_try(try *);
int try_a(try *);
int try_b(try *);
void trial (try *);
#endif
Run Code Online (Sandbox Code Playgroud)
core.h:
#ifndef CORE
#define CORE
struct trytag
{
int a;
int b;
};
#endif
Run Code Online (Sandbox Code Playgroud)
func.c:
#include "api.h"
#include "core.h"
#include <stdlib.h>
try *alloc_try(void)
{
return malloc(sizeof(struct trytag));
}
void free_try(try *t)
{
free(t);
}
int try_a(try *t)
{
return t->a;
}
int try_b(try *t)
{
return t->b;
}
void trial(try *t)
{
t->a = 1;
t->b = 2;
}
Run Code Online (Sandbox Code Playgroud)
main.c中:
#include <stdio.h>
#include "api.h"
int main()
{
try *s_tryvar = alloc_try();
trial(s_tryvar);
printf("a = %d\nb = %d\n", try_a(s_tryvar), try_b(s_tryvar));
free_try(s_tryvar);
}
Run Code Online (Sandbox Code Playgroud)