2016-12-05 85 views
1

時候我已經檢查然而這篇文章C++ function template partial specialization?C++函數模板部分特例錯誤重載運營商

,以下錯誤仍撲朔迷離。爲什麼template-id與函數聲明不匹配?

error: function template partial specialization ‘operator<< <T>’ is not allowed 
ostream& operator<< <T>(ostream& out, RBTree<T>& rbt) 

error: template-id ‘operator<< <int>’ for ‘std::ostream& operator<<(std::ostream&, RBTree<int>&)’ does not match any template declaration 
    friend ostream& operator<< <T>(ostream& out, RBTree<T>& rbt); 

我實現紅黑樹類RBTree任何輸入型T.

class RBTree{ 
    friend ostream& operator<< <T>(ostream& out, RBTree<T>& rbt); 
public: 
    RBTree(){ nil = new RBTreeNode<T>(BLACK); root = nil; } 
    ..... 
}; 


template<class T> 
ostream& operator<< <T>(ostream& out, RBTree<T>& rbt) 
{ 
    rbt.InOrder(rbt.GetRoot(), out); 
    return out; 
} 
+0

C++不允許你以部分專門的函數,該函數名前應刪除。 – sangrey

+0

[重載模板類的朋友操作符<<]可能重複(http://stackoverflow.com/questions/4660123/overloading-friend-operator-for-template-class) –

+0

@sangrey事實上C++不允許專門化成員函數。 – lsbbo

回答

1

,你想實現的功能是不是一個局部的專業化,而是過載。因此,您不應該嘗試使用專業化語法。改變你的運營商實施(注意以下operator<<在第二行沒有<T>):

template<class T> 
ostream& operator<< (ostream& out, RBTree<T>& rbt) 
{ 
    rbt.InOrder(rbt.GetRoot(), out); 
    return out; 
} 
+0

感謝您的幫助。我嘗試刪除運算符<<後的,並且第一個錯誤消失。但是,第二個錯誤「template-id不匹配」仍然存在。 – coder

+0

如果您發佈了所有*真實*代碼,我可能會提供幫助。你迄今共享的內容不會產生這個錯誤 – Smeeheey