C++ 请求非类类型成员
我一直在尝试实现BST,但在编译时遇到以下错误:C++ 请求非类类型成员,c++,binary-search-tree,C++,Binary Search Tree,我一直在尝试实现BST,但在编译时遇到以下错误: bstavl.cpp: In member function ‘bool BSTree::find(int)’: bstavl.cpp:114:15: error: request for member ‘find’ in ‘((BSTree*)this)->BSTree::root->BSTNode::left’, which is of non-class type ‘BSTNode*’ bstavl.cpp:120:16: er
bstavl.cpp: In member function ‘bool BSTree::find(int)’:
bstavl.cpp:114:15: error: request for member ‘find’ in ‘((BSTree*)this)->BSTree::root->BSTNode::left’, which is of non-class type ‘BSTNode*’
bstavl.cpp:120:16: error: request for member ‘find’ in ‘((BSTree*)this)->BSTree::root->BSTNode::right’, which is of non-class type ‘BSTNode*’
我正在实现BSTNode的结构,类BSTree使用BSTNode指针作为根。下面是类和结构的声明:
struct BSTNode {
//---------------------------------------------------------------------
//instance variables
int value;
bool deleted;
struct BSTNode *left;
struct BSTNode *right;
int height;
//---------------------------------------------------------------------
//constructors
//non argumented constructor
BSTNode() {
this->value = 0;
this->height = 0;
this->left = NULL;
this->right = NULL;
this->deleted = false;
}
//given value
BSTNode(int value) {
this->value = value;
this->height = 0;
this->left = NULL;
this->right = NULL;
this->deleted = false;
}
//given value, left pointer, right pointer
BSTNode(int value, BSTNode *left, BSTNode *right) {
this->value = value;
this->height = 0;
this->left = left;
this->right = right;
this->deleted = false;
}
};
//=====================================================================
class BSTree : public BSTNode {
BSTNode *root;
public:
BSTree();
BSTree(int);
bool isEmpty(); //check if the bst is empty
void insert(int newValue); //inserts an int into the bst. Returns success
bool find(int value); //searches bst for int. True if int is in tree
void preorder(); //calls recursive transversal
void inorder(); //calls recursive traversal
void postorder(); //calls recursive transversal
int height(BSTNode *n);
int totalheight(); //returns tot height. height of empty tree = -1
int totaldepth(); //returns tot depth. depth of empty tree = -1
int avgheight(); //returns avg height of tree
int avgdepth(); //returns avg depth of tree
bool remove(int value); //deletes int. returns true if deleted
private:
struct BSTNode* insertRecursive(struct BSTNode *n, int newValue);
void inorderRecursive(BSTNode *n); //traverses tree in inorder
void preorderRecursive(BSTNode *n); //traverses tree in preorder
void postorderRecursive(BSTNode *n); //traverses tree in preorder
};
最后,这里是BSTree::find的实现
bool BSTree::find(int findMe){
if (root->value == findMe)
return true;
else if (findMe < root->value){
if (root->left != NULL)
root->left.find(findMe);
else
return false;
}//else if
else if (findMe > root->value){
if (root->right != NULL)
root->right.find(findMe);
else
return false;
}//else if
}//find
各种各样的事情,包括
(root->right).find(findMe);
root->right->find(findMe);
(root->right)->find(findMe);
还有很多,但在编译时有错误。我知道这可能是一个简单的修复,但我花了几个小时在这个愚蠢的简单函数上,没有任何进展,它真的开始让我沮丧。谢谢 基本上可以归结为这样一个事实,
BSTNode
没有find
方法
而您的find
方法当前使用root
,这在BSTNode
中没有定义
我建议您不要让树从节点继承。因为树不是节点(它只是包含一个节点),所以这种继承不是特别好
而是创建另一个类BSTGeneric
,您的所有树实现都将继承它(像find
这样的方法将在这里,因为它对于所有BST看起来都一样,其他方法可以在这里声明为)
然后,对于每个当前递归函数,添加另一个接受BSTNode*
参数的递归函数,该参数应该是私有的,并让当前函数简单地调用它
因此,不是:
public: void insert(int value)
{
...
root->left->insert(value);
...
}
我们将有:
public: void insert(int value)
{
insert(root, value);
}
private: void insert(BSTNode n, int value)
{
...
insert(n.left, value);
...
}
为什么要添加BSTNode参数?这难道不会违背在类中定义函数的目的吗?我计划实现另一个类AVLTree,我需要用两种不同的方式调用同名函数。这就是为什么我首先将其定义为类方法。既然AVLTree也会使用BSTNode,那就行不通了,是吗?是
BSTree
树BSTree
对象中的所有节点,还是BSTNode
对象?嗯。。。好主意。它们是节点。此外,将变量强制转换为BSTree可以很好地编译,但在将树传递给BSTree时会导致segfaultit@dawkinsjh编辑-大部分重写了我的答案。亲爱的,我会调查的。非常感谢!
public: void insert(int value)
{
insert(root, value);
}
private: void insert(BSTNode n, int value)
{
...
insert(n.left, value);
...
}