Operator overloading on class templates

后端 未结 5 1587
孤城傲影
孤城傲影 2020-12-01 05:47

I\'m having some problems defining some operator overloads for template classes. Let\'s take this hypothetical class for example.

template 
cl         


        
5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-01 06:26

    This helped me with the exact same problem.

    Solution:

    1. Forward declare the friend function before the definition of the class itself. For example:

         template class MyClass;  // pre-declare the template class itself
         template std::ostream& operator<< (std::ostream& o, const MyClass & x);
      
    2. Declare your friend function in your class with "<>" appended to the function name.

         friend std::ostream& operator<< <> (std::ostream& o, const Foo& x);
      

提交回复
热议问题