C++ 我的二进制搜索树的实现不起作用,因为我不断收到错误消息,即没有定义我的节点和typedef项

C++ 我的二进制搜索树的实现不起作用,因为我不断收到错误消息,即没有定义我的节点和typedef项,c++,C++,重要的部分是头文件,因为某种原因,当我将它包括到实现文件中时,它不包括我创建的struct节点或typedef,我是否做错了什么?我以前从来没有遇到过这个问题,也做过很多次。你们有什么建议吗?这是我正在获取的错误。此范围中未声明节点,并且项未命名类型 //Header File #include <iostream> #include <fstream> #include <cstring> #include <iomanip> #include

重要的部分是头文件,因为某种原因,当我将它包括到实现文件中时,它不包括我创建的struct节点或typedef,我是否做错了什么?我以前从来没有遇到过这个问题,也做过很多次。你们有什么建议吗?这是我正在获取的错误。此范围中未声明节点,并且项未命名类型

//Header File
#include <iostream>
#include <fstream>
#include <cstring>
#include <iomanip>
#include <string>
#ifndef BST
using namespace std;
class BST
{
 public:
  typedef int item;

  //constructors
  BST() {root =NULL;}

  // Destructor
  ~BST();

  //Modification member functions
  void reinitialize();
  void insert( const item& entry);
  void remove( const item& target);

  // constant member functions

  bool empty() const{return root == NULL;}
  int length();
  bool present(const item& target);



  // definitions
 private:

  struct Node//my structure
  {
    item data;
    Node *left;
    Node *right;
  };
  Node *root;

  //recursive functions
  void print(Node *p);
  void destroy(Node *r);
  void help_insert(Node *&t, const item& entry);
  void help_remove(Node *&t, const item& entry);
  void remove_node(Node *&t);


};
#define BST

#endif
//头文件
#包括
#包括
#包括
#包括
#包括
#IFNDEFBST
使用名称空间std;
BST级
{
公众:
typedef int项;
//建设者
BST(){root=NULL;}
//析构函数
~BST();
//修改成员函数
void重新初始化();
无效插入(常量项目和条目);
无效删除(常数项和目标);
//常数成员函数
bool empty()常量{return root==NULL;}
int length();
bool当前(施工项目和目标);
//定义
私人:
结构节点//我的结构
{
项目数据;
节点*左;
节点*右;
};
节点*根;
//递归函数
作废打印(节点*p);
空洞破坏(节点*r);
无效帮助插入(节点*&t、常量项和条目);
删除无效帮助(节点*&t、常量项和条目);
无效删除节点(节点*&t);
};
#定义BST
#恩迪夫
源文件

//Implementation of the Binary search tree
//Kyle Ripplinger
#include <cassert>
#include <iostream>
#include <iomanip>
#include "BST.h"
using namespace std;
/*BST::~BST()
{
  destroy(root);
}
void BST::destroy(Node *r)//doesn't work here
{
  if(r!= NULL)
    {
      destroy(r->left);
      destroy(r->right);
      delete r;
    }
}
int BST::length()
{
  return find_length(root);// doesnt work
}
int BST::find_length(Node *r)
{
  if (r== NULL)
    return 0;
  else 
    return find_length(r->left)+1+find_length(r->right);
    }*/
void BST:: insert(const item& entry)
{
  help_insert(root, entry);
}
void BST:: help_insert(Node *&t,const item& entry)
{
  if(t == NULL)
    {
      t = new Node;
      t -> data = entry;
      t -> left = NULL;
      t -> right = NULL;
    }
  else if(entry <t->data)
    help_insert(t->left,entry);
  else
    help_insert(t->right,entry);
}
void BST:: remove(const item& target)
{
  assert(present(target));
  help_remove(root,target);
}
void BST:: help_remove(Node *&t, const item& target)
{
  if {t->data = target)
    remove_node(t);
    else if(target < t->data)
      help_remove(t->left, target);
    else
      help_remove(t->right, target);
}
void BST:: remove_node(Node *&t)
{
  Node *ptr;
  Node *back;
  if(t->left == NULL && t->right == NULL)//leaf
    {
      delete t;
      t = NULL;
    }
  else if(t->left == NULL)//has right child only
    {
      ptr = t;
      t = t->right;
      delete ptr;
    }
  else if(t ->right == NULL)//has left child only
    {
      ptr = t;
      t = t->left;
      delete ptr;
    }
  else//has both children on tree find the leftmost node in the right subtree
    {
      back = t;
      ptr = t->right;
      while(ptr->left != NULL)
    {
      back = ptr;
      ptr = ptr->left;
    }
      t->data = ptr->data;
      if(back == t)
    remove_node(back->right);
      else
    remove_node(back->left);
    }
}
bool BST::present(Item target)
{
  Node *p;
  p = root;
  while(true)
    {
      if(p == NULL)
    return false;
      else if (target < p-> data)
    p = p->left;
      else if(target == p->data)
    return true;
      else
    p= p-> right;
    }
}
/*void BST::print(Node *p)
{
  if(p!=NULL)
    {
      print(p->left);
      cout << p-> data << endl;
      print(p->right);
    }
    }*/
//二进制搜索树的实现
//凯尔·里普林格
#包括
#包括
#包括
#包括“BST.h”
使用名称空间std;
/*BST::~BST()
{
消灭(根);
}
void BST::destroy(Node*r)//在这里不起作用
{
如果(r!=NULL)
{
销毁(右->左);
销毁(右->右);
删除r;
}
}
int BST::length()
{
返回find_length(root);//不起作用
}
int BST::查找长度(节点*r)
{
if(r==NULL)
返回0;
其他的
返回find_length(r->left)+1+find_length(r->right);
}*/
无效BST::插入(常量项和条目)
{
帮助_插入(根,条目);
}
void BST::help_insert(节点*&t、常量项和条目)
{
如果(t==NULL)
{
t=新节点;
t->data=输入;
t->left=NULL;
t->right=NULL;
}
else if(输入数据)
帮助_插入(t->左,输入);
其他的
帮助_插入(t->右,输入);
}
无效BST::删除(常量项和目标)
{
断言(当前(目标));
帮助您删除(根、目标);
}
void BST::help_remove(节点*&t、常量项和目标)
{
如果{t->data=target)
移除_节点(t);
否则如果(目标数据)
帮助_移除(t->左,目标);
其他的
帮助_移除(t->右侧,目标);
}
void BST::删除节点(节点*&t)
{
节点*ptr;
节点*返回;
如果(t->left==NULL&&t->right==NULL)//leaf
{
删除t;
t=零;
}
如果(t->left==NULL)//只有右子级,则为else
{
ptr=t;
t=t->右;
删除ptr;
}
如果(t->right==NULL)//仅具有左子级,则为else
{
ptr=t;
t=t->左;
删除ptr;
}
else//让树上的两个子节点在右子树中找到最左边的节点
{
back=t;
ptr=t->右;
while(ptr->left!=NULL)
{
返回=ptr;
ptr=ptr->左;
}
t->data=ptr->data;
如果(返回==t)
删除_节点(后->右);
其他的
删除_节点(后->左);
}
}
bool BST::当前(项目目标)
{
节点*p;
p=根;
while(true)
{
if(p==NULL)
返回false;
否则如果(目标数据)
p=p->左;
否则如果(目标==p->数据)
返回true;
其他的
p=p->右;
}
}
/*void BST::打印(节点*p)
{
如果(p!=NULL)
{
打印(p->左);
cout(数据权);
}
}*/

编译器是正确的。由于您的include-guard,您的

void BST::insert(const item& entry)
变成

void ::insert(const item& entry)
在全局上下文中没有称为
项的类型


调试提示:为了尝试将编译器指向正确的方向,我将声明的第一行更改为

void BST::insert(const BST::item& entry)

当编译器报告“
error:'item'in namespace':::”没有命名类型时,很明显
BST
不知怎么消失了。

在标题末尾,您将
BST
定义为一个宏,扩展为空。然后
int-BST::find_length(Node*r)
扩展为
int::find_length(节点*r)
-这定义了一个常规的非成员函数。因为它不在类
BST
的范围内,所以节点的名称
是不可见的。你究竟为什么决定定义那个宏?这里的要点是在重用标识符时要非常小心。是的。命名约定真的很有帮助。我喜欢在e包括防护装置——并且仅包括防护装置——以
\u H
\u HPP
结尾。