c++ - 我在 C 中有相互依赖的 header ,这给了我错误。我怎样才能删除它?

标签 c++ c header

tree.h 文件:

#ifndef tree
#define tree

#include "common.h"
#include "set.h"

/*Tree Structure*/
typedef struct
{
    Set *set;
    unsigned int setLength;
    int vertexLength;
    int *vertex;
    int idle;
    int parent;
    int level;
    int numberOfChildren;
} TreeNode;

typedef struct
{
    TreeNode* node;
    int nodeLength;
    int height;
} Tree;

Tree* fill_tree(char *filename, int worldSize, int rank);
int get_number_of_leaves(Tree *tree);
TreeNode get_tree_node(int index, Tree* tree);
TreeNode* get_tree_nodes(Tree* tree);
int get_tree_nodes_length(Tree* tree);
void compute_level(int u, Tree* tree);
void print_tree(Tree* tree);
int get_max_level(Tree* tree);
int* create_task_list(Tree* tree, int* taskListLength, int rank);
int* map(int* taskList, int taskListLength, int* mapLength, int rank);
void generate_combination_sets(Tree* tree, int nodePerProcess, int rank);

#endif

set.h 文件:

#ifndef set
#define set

#include "common.h"
#include "tree.h"

/*Tree Structure*/
typedef struct
{
    int combinationLength;
    int* combination;
} Set;

Set* generate_subsets(int* vertex, int vertexLength, unsigned int pow_set_size, int rank);
Set* match(Set* parent, unsigned int parentLength, Set* child, unsigned int childLength, TreeNode childNode, TreeNode parentNode, unsigned int* solutionLength, Graph* g, int rank);
int* union_operation(int* parenteCombination, int parentCombinationLength, int* childCombination, int childCombinationLength, unsigned int* mergedLength, int rank);
int not_in_solution(Set* solution, int solutionLength , int* merged, unsigned int mergedLength);
void print_set(Set set);

#endif

common.h 文件:

#ifndef common
#define common

// Libraries

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <pthread.h>
#include <limits.h>
#include "mpi.h"
#include "graph.h"
#define true 1
#define false 0
#endif

我有错误,树和集合头文件是相互依赖的,编译器给我一个错误,在 tree.h 中找不到集合,对于集合也是如此。我应该在 common.h 文件中包含所有字段吗?

最佳答案

当你需要引用另一个头文件中的结构时使用前向声明,头文件不需要知道结构的细节,在tree.h中试试这个:

 #ifndef tree
 #define tree

 #include "common.h"
 // #include "set.h"  <-- don't include set.h

 typedef struct Set Set;

 /*Tree Structure*/
 typedef struct
 {
     Set *set;
     unsigned int setLength;
     int vertexLength;
     ...
 } TreeNode;

set.h中:

typedef struct Set Set;
struct Set
{
    int combinationLength;
    int* combination;
} Set;

关于c++ - 我在 C 中有相互依赖的 header ,这给了我错误。我怎样才能删除它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36884532/

相关文章:

c++ - getline 并立即测试 EOF

curl - 如何从 cURL 命令实现 jmeter 请求发布

c - "multiple definition"的问题

c++ - 包括<Windows.h>导致(未知属性“no_init_all”)错误

c - 指向 char 数组的指针数组

c++ - 组合和聚合是一种关联形式吗?

c++ - IPPROTO_SCTP、SCTP_STATUS 的 getsockopt "Invalid argument"

c++ - memcpy 极其意外的行为

Cygwin:Libcurl 不工作

c - vm_area_struct 中的 unsigned long vm_pgoff