2011-12-05 68 views
2

一個基本的二叉樹我的頭文件包括以下內容:返回一個指向類定義的結構,多範圍經營

template<typename T> 
    class Btree{ 
     // OVERVIEW: a binary tree with flexible structure that is not sorted 

    private: 

     struct node {   //a container object 
      node *left;  //left and right tree 
      node *right; 
      T *o;  // pointer to object of node 
     }; 

    public: 

     node *root; //pointer to the root of the tree (NULL if empty) 

     node* insert (node *parent, T *child, int child); 
     //MODIFIES: this 
     //EFFECTS: creates a node that stores a pointer to the new child 
     //  and returns the pointer to the node of the new child  
     //  the integer child is either 0, for left child, 
     //  or anything else for right child  

      // void printTree (node * root); 
     //EFFECTS: takes the root of a tree and prints the tree's 
     //  coordinates 

     Btree(){}; //ctor 
     Btree(){} //dtor 


    }; 

    #include "btree.cpp" 

我的.cpp看起來是這樣的,並注意它是包含在我的頭的底部,以避免模板編譯器錯誤:

template <typename T> 
    Btree<T> :: node * Btree<T>::insert (node *parent, T *child, int child) 

    { 
     node *np = new node; 
     np-> o = child; 
     np->left = NULL; 
     np->right = NULL; 
     if (child == 0) 
      parent->left = np; 
     else 
      parent->right = np; 
     return np; 
    } 
然而

,我得到以下編譯器錯誤:

btree.cpp:3: error: expected constructor, destructor, or type conversion before ‘*’ token

我在用g ++編譯版本4.1.2。

任何人都可以幫忙嗎?

+1

這是C++,對不對?你能用這個來重新嗎?你必須擺脫你的一個標籤,但你的問題會被更合適的觀衆看到。 –

+0

*我的.cpp看起來像這樣,而不是它包含在我的標題底部以避免模板編譯器錯誤* Ehm是什麼? – FailedDev

+0

請不要再添加這些標籤!他們不會幫助。 C++就夠了。 – Beginner

回答

3

首先,你的析構函數應該在它前面~,所以更改

Btree(); //ctor 
Btree(){} //dtor 

Btree(); //ctor 
~Btree(){} //dtor 

其次,你的insert返回類型之前需要typename,因爲它是依賴類型:

template <typename T> 
    typename Btree<T>::node* Btree<T>::insert(node *parent, T *child, int child) 
// ^^^^^^^^ <- needed 
    { 
    node *np = new node; 
    np-> o = child; 
    np->left = NULL; 
    np->right = NULL; 
    if (child == 0) 
     parent->left = np; 
    else 
     parent->right = np; 
    return np; 
    } 

A所以你需要重新命名你的一個參數,你有兩個名字child

+0

對不起,我的意思是,但謝謝 – Layla

+0

@LeilaHejazi這樣回答你的問題?對不起,我無法分辨您的意思是否正確或不正確。 –

+0

是的,這回答了我的問題。 typename是我正在尋找的關鍵字。它現在編譯。 – Layla

相關問題