C++作业 - 二叉搜索树帮助

3
首先声明,这是一份作业,我只是真的需要帮助来完成二叉搜索树。
程序要展示多态性,使用人作为抽象基类,和继承Person的其他类型的人。每个人都有一个姓氏,我正在尝试使用二叉搜索树按姓氏字母排序人员。
我已经编写了我认为应该是可接受的二叉搜索树,但我仍然遇到错误。二叉搜索树只需要具有插入和遍历函数,应该是递归的。
我遇到的错误是:Error 19 error C4430: missing type specifier - int assumed bst.cpp 这在第51、64和70行发生。这是我的代码:
头文件:
#ifndef BST_H
#define BST_H

template <class T>
class BST
{
    private:
        class BinNode
        {
            public:
                BinNode(void);
                BinNode(T node);

                BinNode *left;
                BinNode *right;
                T data;
        };

        BinNode* root;

    public:
        BST();   
        ~BST();

        void insert(const T &);
        void traverse();
        void visit(BinNode *);


    //Utlity Functions
    private:
        void insertAux(BinNode* &, BinNode *);
        void traverseAux(BinNode *, ostream &);
};

#include "BST.cpp"
#endif

实现文件:

 #include <iostream>
#include <string>

using namespace std;

#ifdef BST_H

template <class T>
BST<T>::BinNode::BinNode()
{
    left = right = 0;
}

template <class T>
BST<T>::BinNode::BinNode(T node)
{
   left = right = 0;
   data = node;
}

template <class T>
BST<T>::BST()
{
    root = 0;
}

template <class T>
void BST<T>::insertAux(T i, BinNode* &subRoot)
{
    //inserts into empty tree
    if(subRoot == 0)
        subRoot = new BinNode(i);
    //less then the node
    else if(i<subRoot->data)
        insertAux(i, subRoot->left);
    //greater then node
    else
        insertAux(i, subRoot->right);
}

template <class T>
void BST<T>::insert(const T &i)
{
    insertAux(T i, root)
}

template <class T>
BST<T>::traverse()
{
    traverseAux(root);
}

template <class T>
BST<T>::traverseAux(BinNode *subRoot)
{
    if (subRoot == 0)
        return;
    else
    {
        traverseAux(subRoot->left);
        visit(subRoot);
        traverseAux(subRoot->right);
    }       
}

template <class T>
BST<T>::visit(BinNode *b)
{
    cout << b->data << endl;
}

#endif

如果有人能够快速浏览一下这个内容并给我一些提示吗?它现在真的让我很困惑。谢谢!

1
请明确指出您遇到错误的行数,不要让我们去数。 - Greg Hewgill
1
缺少类型说明符 - 我想知道它缺少了什么 :) - David Titarenco
2个回答

3
你在一些函数定义中省略了返回类型。
例如:
template <class T>
BST<T>::traverse()
{
    traverseAux(root);
}

should be:

template <class T>
void BST<T>::traverse()
{
    traverseAux(root);
}

1
你应该将 BST<T>::traverse() 改为 void BST<T>::traverse()
类似其他错误。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接