我有以下文件:第一个是 data_structs.c,第二个是相应的头文件 data_structs.h。存在一种“循环依赖”问题,因为在我的 .c 中,我定义了两个使用结构作为参数的函数,也在主体中定义了两个函数,在我的 .h 中,如果我是对的,我需要所有结构定义才能让它们是公开的并且可以从其他源文件中使用。我该如何解决它?
.c
#include <stdlib.h>
void appendResultsList(struct ResultsList* list, char* result){
struct ResultsListElement *new = malloc(sizeof (struct ResultsListElement));
new->value = result;
new->next = NULL;
list->last->next = new;
list->last = new;
}
void appendRotationsList(struct RotationsList* list, struct RotationNode* rotation_node){
struct RotationsListElement *new = malloc(sizeof (struct RotationsListElement));
new->value = rotation_node;
new->next = NULL;
list->last->next = new;
list->last = new;
}
.h
#ifndef DATA_STRUCTURES
#define DATA_STRUCTURES
#include "data_structures.c"
struct ResultsList {
struct ResultsListElement* first;
struct ResultsListElement* last;
};
struct ResultsListElement {
char* value; //il matching
struct ResultsListElement* next;
};
struct RotationList { //list_el
char man;
char woman;
struct RotationList* next;
};
struct RotationNode {
struct RotationList* rotation;
int index;
int missing_predecessors;
struct SuccessorsList* successors;
};
struct SuccessorsList {
struct RotationNode* value;
struct SuccessorsList* next;
};
struct RotationsList { //free_rotations_list
struct RotationsListElement* first;
struct RotationsListElement* last;
};
struct RotationsListElement { //free_rotations_list
struct RotationNode* value;
struct RotationsListElement* next;
};
void appendResultsList(struct ResultsList*, char*);
void appendRotationsList(struct RotationsList*, struct RotationNode*);
#endif
我认为解决方案可以在data_structs.h(结构+函数)中全部定义,但我认为这不好。我只想要 .h 中的函数声明而不是定义。预先感谢。
您已将包含内容倒退了。如果您曾经包含过 .c 文件,那么它几乎总是错误的。
您想将 .h 文件包含在 .c 文件中:
#include <stdlib.h>
#include "data_structures.h"
void appendResultsList(struct ResultsList* list, char* result){
struct ResultsListElement *new = malloc(sizeof (struct ResultsListElement));
new->value = result;
new->next = NULL;
list->last->next = new;
list->last = new;
}
void appendRotationsList(struct RotationsList* list, struct RotationNode* rotation_node){
struct RotationsListElement *new = malloc(sizeof (struct RotationsListElement));
new->value = rotation_node;
new->next = NULL;
list->last->next = new;
list->last = new;
}
并从头文件中删除
#include
。