请求非类类型成员
Request for non-class type member
我一直在尝试实现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: 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);
(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);
...
}
相关文章:
- 根据模板类型选择类模板的成员类型?
- 函数模板签名中忽略的成员类型def 的访问说明符
- 在C++如何从数组中提取成员并返回成员类型的数组?
- C++从成员类型中扣除类型的功能模板?
- 无法初始化以 std::byte 作为成员类型的位字段
- 构造函数可以更改默认成员类型吗?
- 使用相同方法但不同成员类型构建类的最佳方法
- 允许通过指向方法的成员参数的指针来推断模板参数所指向的成员类型
- 使用 std::条件根据模板参数选择成员类型
- CRTP:如何推断要用作返回类型的成员类型?
- 对成员类型的成员方法使用 std::result_of<>
- C :可以从类及其受保护的成员类型继承可以继承吗?
- 如何在不指定不必要的模板参数的情况下使用模板类的成员类型
- 基类数据成员类型取决于派生类
- C 多态性:允许模棱两可的成员类型
- 使用使用成员类型别名的构造函数来推论类模板参数
- 获取与在模板参数中传递的函数成员类型相同的类
- 如果静态成员未初始化并且成员类型是类本身,该怎么办?
- boost::bind 无法绑定到纯虚拟基类中定义的非静态函数模板成员类型
- 将联合强制转换为其成员类型之一