2016-09-29 25 views
5

我試圖實現一個設置基於二叉樹搜索。所以我建立這個集合從一個(指向節點),其中節點有一個值,左側和右側的孩子(都指向節點也)。所以這樣我可以通過指向root->右側創建的節點等來設置根節點右側的新節點。看看定義:對象沒有命名一個類型 - C++

template <class T> 
class Set 
{ 
    public: 
     Set(); 
     ~Set(); 
     void push(const T&); 
     bool belongs(const T&) const; 
     void remove(const T&); 
     const T& min() const; 
     const T& max() const; 
     unsigned int cardinal() const; 
     void show(std::ostream&) const; 

     friend ostream& operator<<(ostream& os, const Set<T> &c) { 
      c.show(os); 
      return os; 
     } 

    private: 

     struct Node 
     { 
      Node(const T& v); 
      T value; 
      Node* left; 
      Node* right; 
     }; 

     Node* root_; 
     int cardinal_; 

    Node & fatherOfNode(const Node & root, const T & key, const bool hook) const; 

}; 

... 

// This function is the one with errors. 
template <class T> 
Node & Set<T>::fatherOfNode(const Node & root, const T & key, const bool hook) const { 
    // Some code 
} 

所以我有這個錯誤:

/home/jscherman/ClionProjects/algo2-t3-bts/set.hpp:247:1: error: ‘Node’ does not name a type 
Node & Set<T>::fatherOfNode(const Node & root, const T & key, const bool hook) const { 
^ 

我已經看到了很多與此錯誤相關的帖子,但大多是由寫作造成的函數實現在其定義之前。正如你所看到的,fatherOfNode的實現低於它的定義,所以它似乎不是我的情況。

有關發生了什麼的任何想法?

+2

它應該是'Set :: Node'。 –

+0

這個問題有什麼問題?我認爲我已經足夠明確和清楚了。你能告訴我我做錯了什麼,所以我將來不會重複嗎? – jscherman

回答

4

NodeSet一個內部類,所以這個類之外,你需要與解決此問題:

Set<T>::Node 

所以你的函數定義將需要:

template <class T> 
typename Set<T>::Node & Set<T>::fatherOfNode(const Set<T>::Node & root, const T & key, const bool hook) const { 

Here it is, working.

+0

Ohhhh我沒有'typename'試過這個。謝謝! – jscherman