【发布时间】:2019-05-04 00:31:05
【问题描述】:
请注意,关于 SO 的循环依赖有几个问题(包括我自己问过的一个),但我觉得没有一个问题能帮助我解决这个特殊问题。
请考虑以下两个文件:
table.h
#ifndef s_table_h
#define s_table_h
#include "value.h"
#include "object.h"
typedef struct {
ObjectString* key;
Value value;
} Entry;
typedef struct {
int capacity;
int count;
Entry* entries;
} Table;
void initTable(Table* table);
void setTable(Table* table, ObjectString* key, Value value);
bool getTable(Table* table, ObjectString* key, Value* out);
#endif
object.h
#ifndef s_object_h
#define s_object_h
#include "common.h"
#include "table.h"
typedef enum {
OBJECT_STRING
} ObjectType;
typedef struct {
ObjectType type;
Table attributes;
} Object;
typedef struct {
Object base;
char* chars;
int length;
} ObjectString;
bool stringsEqual(ObjectString* a, ObjectString* b);
#endif
如您所见,这两者相互依赖:table.h 需要ObjectString*,而object.h 需要具体的Table。两个对应的.c 实现文件分别访问ObjectString* 和Table 的具体成员。
解决此问题的推荐方法是什么?通常,在 C 中解决这类问题的常用方法是什么?
请仅解决技术方面的问题,而不是软件设计方面的问题。
【问题讨论】:
-
table.h 不需要
objectstring它只需要objectstring*可以留待以后解决..
标签: c header circular-dependency