1

I have checked this post C++ function template partial specialization?

However, the following error is still confusing. Why does template-id not match the function declaration?

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);

I am implementing the red-black tree with class RBTree for any input type 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;
}
Community
  • 1
  • 1
thinkdeep
  • 945
  • 1
  • 14
  • 32
  • C++ does not allow you to partially specialize a function, you should remove the before the function name. – sangrey Dec 05 '16 at 11:44
  • Possible duplicate of [overloading friend operator<< for template class](http://stackoverflow.com/questions/4660123/overloading-friend-operator-for-template-class) – Jean-Baptiste Yunès Dec 05 '16 at 11:47
  • @sangrey in fact c++ does not allow specializing a member function. – lsbbo Dec 05 '16 at 12:00

1 Answers1

1

The function that you're trying to implement is not a partial specialization, but rather an overload. As such you should not be attempting to use specialization syntax. Change your operator implementation to (note the absence of <T> following operator<< on second line):

template<class T>
ostream& operator<< ( ostream& out, RBTree<T>& rbt)
{
    rbt.InOrder( rbt.GetRoot(), out );
    return out;
}
Smeeheey
  • 9,906
  • 23
  • 39
  • thanks for your help. I tried remove the following the operator <<, and the 1st error disappears. However, the 2nd error "template-id mismatch" is still there. – thinkdeep Dec 05 '16 at 12:32
  • If your post all your *actual* code I might be able to help. What you shared so far would not produce this error – Smeeheey Dec 05 '16 at 12:36