我有这样的情况:
啊和Bh
在啊:
typedef struct TypeA TypeA_t;
struct TypeA {
...
TypeA_t *a;
void (*doSomething)(TypeB_t *);
};
Run Code Online (Sandbox Code Playgroud)
在博赫:
typedef struct TypeB TypeB_t;
struct TypeB {
...
TypeB_t *b;
TypeA_t something;
};
Run Code Online (Sandbox Code Playgroud)
在每个文件中包含头文件的正确方法是什么?
如果我包含A.hinB.h和B.hinA.h我得到:
error: unknown type name 'TypeB_t'在A.h
和
error: unknown type name 'TypeA_t'在B.h
我在这里发现了一个类似的问题,但它不适用于我的情况。
我正在尝试用 C 语言创建一个“类模型”,其中我定义了一个struct代表类的对象,并在其中定义了函数指针来代表方法,如下所示:
//ClassName.h
typedef struct struct_ClassName ClassName;
struct ClassName
{
char a;
char b;
char c;
void (*method1)(ClassName*, char);
void (*method2)(ClassName*, char);
...
void (*methodN)(ClassName*, char);
};
void initClassName(ClassName*);
Run Code Online (Sandbox Code Playgroud)
//ClassName.c
#include "ClassName.h"
static void method1(ClassName *this_c, char c);
static void method2(ClassName *this_c, char c);
...
static void methodN(ClassName *this_c, char c);
void initClassName(ClassName *this_c)
{
this_c->method1 = &method1;
this_c->method2 = &method2;
...
this_c->methodN = &methodN;
}
void method1(ClassName *this_c, char c)
{
//do something
}
void method2(ClassName …Run Code Online (Sandbox Code Playgroud)