2013-10-19 64 views
0

所以我得到這個錯誤,這與我的AVLTree類繼承我的BTree類的方式有關。據我所知,編譯器的行爲好像找不到BTree.h(位於同一目錄中),否則就是不喜歡我包含它的方式。繼承問題 - '{'令牌之前的預期類名稱

我一直在網上尋找,但是我看到的這個問題的大多數例子都是有人忘記包含某些東西,所以我不確定接下來要做什麼。

編輯:我還添加了代碼示例爲BTree.h

任何援助將不勝感激。

g++ -g -w -Wall TreeTest.cpp BTree.h BinaryTree.h AVLTree.h -o testTrees In file included from TreeTest.cpp:12:0: AVLTree.h:22:29: error: expected class-name before '{' token TreeTest.cpp: In function

這裏是代碼:

#ifndef AVLTree_H 
#define AVLTree_H 

#include <vector> 
#include <iostream> 
#include "BTree.h" 

using std::vector; 
using std::cout; 
using std::string; 

template <class T> 
class AVLTree : public BTree{ 
    public: 
     struct TreeNode{ 
      TreeNode * leftChild, 
       * rightChild; 
      T key; 
      vector<T> data; 
      int size; 
      int height; 
      bool deleted; 
     }; 

     //Standard tree functions 
     AVLTree(); 
     virtual ~AVLTree(); 
     virtual bool isEmpty(); 
     virtual int getSize(); 
     virtual int getHeight(); 
     virtual int insert(T key, T data); //returns number of insert calls 
     virtual int remove(T key);   //returns number of remove calls 
     virtual int contains(T key);  //removes number of contains calls, or 0 if doesn't exist 
     virtual std::vector<T> getData(T key); 

     //Special functions 
     virtual void displayAll(); 
     virtual double getAverageDepth(); 
     virtual int getTotalIPL(); //Retrieves internal path length from root 

    private: 
     int size; 
     TreeNode * root; 

     int * contains(T key, TreeNode * node, int calls); 
     int insert(T key, T data, TreeNode *& node, int calls); 
     int remove(T key, TreeNode *& node, int calls); 
     int getDepth(TreeNode*curr, int total); 
     int getIPL(TreeNode * start, int level); 
     void rotateLeft(TreeNode *& node); 
     void doubleLeft(TreeNode *& node); 
     void rotateRight(TreeNode *& node); 
     void doubleRight(TreeNode *& node); 
     int max(int a, int b); 
     int getHeight(TreeNode * t); 
     void display(TreeNode * node, string indent, bool last); 
     TreeNode * makeNode(T key, T data); 
     TreeNode * getNode(T key, TreeNode * node); 
     void destroySubTree(TreeNode * start); 
}; 

這裏是BTree.h:

#ifndef bTree_H 
#define bTree_H 

#include <vector> 

template <class T> 
class BTree{ 
    public: 
     struct TreeNode{ 
      TreeNode * leftChild, 
       * rightChild; 
      T key; 
      std::vector<T> data; 
      int size; 
     }; 

     //Standard tree functions 
     virtual ~BTree(); 
     virtual bool isEmpty() = 0; 
     virtual int getSize() = 0; 
     virtual int getHeight() = 0; 
     virtual int insert(T key, T data) = 0; //returns number of insert calls 
     virtual int remove(T key) = 0;   //returns number of remove calls 
     virtual int contains(T key) = 0;   
     virtual std::vector<T> getData(T key) = 0; 

     //Special functions 
     virtual void displayAll() = 0; 
     virtual double getAverageDepth() = 0; 
     virtual int getTotalIPL() = 0; //Retrieves internal path length from root 

    private: 
     int size; 
     TreeNode * root; 

}; 

template <class T> 
BTree<T>::~BTree<T>(){} 

#endif 
+0

因此,該行被它指向? –

+0

AVLTree.h:22:29:錯誤:期望'{'標記之前的類名。這是第22行,我寫了「AVLTree類:公共BTree {」 – SemperCallide

+0

您能顯示Btree.h的內容嗎? –

回答

2

由於B樹是一個類模板,你不能繼承它直接,但有指定它的實例化。那就是你必須提供模板參數,在你的情況下應該是AVLTree實例化的同一類型。

template <class T> 
class AVLTree:public BTree<T> 
相關問題