【发布时间】:2015-06-30 03:41:31
【问题描述】:
我正在编写类中的所有方法原型,它们的定义将在它之外。这是我的 AVL 课程设置:
template <class type>
class avlTree : public binarySearchTree<type>
{
public:
avlTree(); //default constructor
~avlTree(); //destructor
const type & findMin() const;
const type & findMax() const;
bool isEmpty() const;
void printTree() const;
void makeEmpty();
void insert(const type & newData);
void remove(const type & deleteItem);
private:
template<class type>
struct avlNode
{
type info;
avlNode *left;
avlNode *right;
int height;
avlNode(const type & data, avlNode *ll, avlNode *rl, int h = 0)
: info{ data }, left{ ll }, right{ rl }, height{ h } {}
};
avlNode<type> * root;
void insert(const type & newData, avlNode<type> * & p);
void remove(const type & deleteItem, avlNode<type> * & p);
avlNode<type>* findMin(avlNode<type> * p); //these two methods are where I'm having problems.
avlNode<type>* findMax(avlNode<type> * p);
};
我在编写内部(私有)findMin() 和 findMax() 定义时遇到问题。为了清楚起见,不是实际的算法,而是返回 avlNode 对象指针的语法。类中的原型没有显示错误,但是当我尝试在类之外编写它的定义时,Intellisense 不会显示,并且当我尝试编码 p->member 时本地 p 指针未显示其成员。通常 Intellsense 会显示下拉菜单及其成员,但它没有显示。所以我知道存在某种语法错误。我认为这可能与模板的设置方式有关,但我不确定。那我做错了什么?
我遇到问题的方法定义:
template <class type>
typename avlTree<type>::avlNode* avlTree<type>::findMin(avlNode * p)
{
if (p == nullptr)
return nullptr;
if (p->left == nullptr) //when I hover over 'p->left' that's when Intellisense says '<unknown> avlTree<type>::avlNode::left'
return p;
return findMin(p->left); //same thing here
}
【问题讨论】: