2010-12-06 49 views
3

對於初學者來說,這是作業,我真的需要二叉搜索樹的幫助。C++作業 - 二進制搜索樹幫助

該程序是顯示多態性,使用人作爲抽象基類,以及其他類型的人繼承人。每個人都有一個姓氏,我試圖用一個二叉搜索樹來按姓氏字母排序。

我寫了我認爲應該是一個可以接受的二叉搜索樹,但我仍然收到錯誤。二叉搜索樹只需要具有插入和遍歷功能。這應該是遞歸的。

我得到的錯誤是:錯誤19錯誤C4430:缺少類型說明符 - 假定爲int 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

請清楚指出您所犯錯誤的行。不要讓我們數。 – 2010-12-06 00:29:06

回答

3

您省略了某些函數定義的返回類型。

例如:

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

應該是:

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

你應該改變BST<T>::traverse()void BST<T>::traverse()

與其他類似的錯誤回報。