gpt4 book ai didi

c++ - Eclipse 提示递归函数调用

转载 作者:行者123 更新时间:2023-11-28 03:19:09 25 4
gpt4 key购买 nike

一个简单的二叉搜索树类声明:

#include <vector>
#include <stdio.h>

// Provides various structures utilized by search algorithms.

// Represents an generalized node with integer value and a set of children.
class Node {
protected:
std::vector<Node*> children;
int value;
public:
//Creates a new instance of a Node with a default value=-1.
Node(){value = -1;}
//Creates a new instance of a Node with a specified value.
explicit Node(int value){this->value = value;}
virtual ~Node(){delete children;}

//Adds new Node with specified value to the list of child nodes. Multiple
//children with the same value are allowed.
//Returns added node.
virtual Node* Insert(int value);
//Removes first occurrence of a Node with specified value among children.
virtual void Remove(int value);
};

// Represents a binary search tree node with at most two children.
class BTNode: public Node {
public:
//Creates a new instance of a BTNode with a default value=-1.
BTNode():Node(){}
//Creates a new instance of a BTNode with a specified value.
explicit BTNode(int value):Node(value){}

//Adds new BTNode with specified value to the list of child nodes in an
//ordered manner, that is right child value is >= value of this node and
//left child value < value of this node.
virtual BTNode* Insert(int value);
//Removes first occurrence of a Node with specified value from the tree.
virtual void Remove(int value);
//Returns a node with specified value.
virtual BTNode* Search(int value);
};

eclipse 提示它的定义:

BTNode* BTNode::Search(int value){
if (this->value == value) return *this;

//Determines whether value is in left(0) or right(1) child.
int child = this->value > value ? 0 : 1;
if (children[child] != NULL)
return children[child]->Search(value);

return NULL;
}

调用 children[child]->Search(value) 的确切位置,并显示消息“无法解析方法搜索”。 构建运行良好(没有任何编译错误)。这有什么问题?

P.S.:还没有尝试运行代码。正在努力。

最佳答案

SearchBTNode 的一部分接口(interface),但它不是 Node 的一部分s 接口(interface),childrenvectorNode*所以调用 Search 是无效的在 Node * 上.如果对 Node 有意义有一个Search方法然后将其添加到 Node会解决这个问题。如果不是,那么您需要重新考虑您的设计,这可能超出了这个问题的范围。

还有一些其他问题。你有:

virtual ~Node(){delete children;}

但是children不是 pointer这是一个std::vector<Node*> .您需要遍历 vector并调用delete每个元素。在 Search你有这个:

if (this->value == value) return *this;

但是Search返回 BTNode*所以应该是:

if (this->value == value)  return this ;

关于c++ - Eclipse 提示递归函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15972507/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com