Eclipse抱怨递归函数调用
Eclipse complains about recursive function call
一个简单的二叉搜索树类声明:
#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)
发生的位置,并显示消息"method Search无法被解析"。构建运行良好(没有任何编译错误)。这有什么问题吗?
注:我还没有试着运行代码。正在努力
Search
是BTNode
接口的一部分,但它不是Node
接口的一部分,children
是Node*
的vector
,因此在Node *
上调用Search
是无效的。如果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 ;
相关文章:
- 如何在Elixir中调用递归函数并行
- 我在 C++ 中创建了一个函数来递归反转字符串,但是之后如何使功能打印一个 endl?
- 如何通过函数指针递归调用类成员函数?
- 使用带有一个参数函数的递归找到数字的平方
- 返回不停止函数,递归函数问题?(编程练习,动态规划,Levenshtein 回溯)
- 对可变参数函数的递归调用的链接器错误
- 从 C++ 中的函数返回递归 lambda
- 函数以递归逆转链接的列表
- C - 为什么合并函数在递归调用后会逆转数组
- 系统堆栈中的两个函数的递归调用(将不同数量的数组作为参数传递)有什么区别
- 对具有传递引用参数的函数使用递归
- (C++)对文件输入函数的递归调用
- 二叉树析构函数的递归调用
- 在循环中调用递归函数
- 如何计算对该函数的递归调用,以及正确答案是什么
- ambigus调用递归sudoku_backtracker函数
- 如何在C++中调用递归链表遍历函数
- 为什么在NULL检查之后要在指针上调用递归函数?
- 通过结构调用递归函数
- 如果在指令之前调用递归函数怎么办