【问题标题】:Possible to have a class member variable BST within a class used for BST?在用于 BST 的类中可能有一个类成员变量 BST?
【发布时间】:2018-10-11 05:49:14
【问题描述】:

我有一个使用模板的二叉搜索树类。所以我可以创建一个 Faculty 类型的 BST,但是有没有办法在 Faculty 类中包含一个 BST?

我想在 Faculty 类中保存一个整数树作为成员变量。

所以我会有一个 Faculty 树,并且每个节点(类型为 Faculty)都有自己的 Integer 树。

这是我尝试添加 BST 成员变量的 Faculty 类。从我在其他地方读到的内容来看,问题在于尝试#include BST.h 文件,因为我已经在 BST 文件中包含了 Faculty 文件。

#ifndef Faculty_H
#define Faculty_H

#include "Person.h"
#include "BST.h"


using namespace std;

class Faculty : public Person
{

public:


Faculty();
Faculty(int new_ID);
~Faculty();


friend ostream& operator<<(ostream& os, Faculty& f);



private:


BST<int> advisees;

};






#endif //Faculty_H

这是我的错误: ./Faculty.h:36:2:错误:未知类型名称“BST” BST 建议;

这是 BST.H

#ifndef BST_H
#define BST_H

#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdlib>

#include "tree_node.h"
#include "tree_node.cpp"

#include "Person.h"
#include "Student.h"
#include "Faculty.h"

using namespace std;




template <class T>
class BST
{

public:

BST();
~BST();


void insert(T k);
tree_node<T>* find(T k);
bool contains(T k);
bool delete_node(T k);

tree_node<T> *get_min();
tree_node<T> *get_max();

tree_node<T> *get_root();

bool is_empty();
int get_size();
void print_tree(tree_node<T> *node);

tree_node<T>* get_successor(tree_node<T> *d);




private:

tree_node<T> *root;
unsigned int size;
};



#endif //BST_H

【问题讨论】:

  • BST.h的内容是什么?
  • 将在编辑中添加

标签: c++ search tree binary binary-search-tree


【解决方案1】:

你有一个#include 循环; BST.h 包括Faculty.h,其中包括BST.h。这会导致很多问题,包括你现在看到的;根据包含顺序,当教师需要使用 BST 时,可能无法定义 BST。

BST.h 不需要了解有关人员、学生或教师的任何信息。删除这些包含应该可以解决当前的问题。

【讨论】:

  • 非常感谢!那成功了。我同意 BST 没有理由了解这些其他课程。我只需要修改#includes,它就会再次编译。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-17
  • 1970-01-01
  • 1970-01-01
  • 2010-11-26
相关资源
最近更新 更多