How to implement overload operator with template in cpp

It says that "In file included from stack.cpp:6: ./stack.hpp:41:25: error: no candidate function template was found for dependent friend function template specialization friend ostream& operator << <>(ostream& out, const Stack& s1);

                    ^"

//hpp

    class Stack {
    public:
        template <class U>
        Stack<T>& operator = (const Stack<U>& a_stack);
        friend ostream& operator << <>(ostream& out, const Stack<T>& s1);
    private:
        T top;
        
    };```
/cpp
    ```template<typename U>
    ostream& operator << (ostream& out, const Stack<U>& s1)
    {
    cout << "\n ** hi";
  
    return out;
    }


    template<class T> template<class U>
    Stack<T>& Stack<T>::operator=( const Stack<U>& a_Stack){
    if(top == a_Stack.top) 
    {
     return;
    }
    char next;
    while(!empty())
    {
    next = pop();

    }
     if(a_Stack.top == NULL)
    {
    top = NULL;
      return;
  }
     StackFramePtr<T> temp = a_Stack.top;
     StackFramePtr<T> end;
     end = new StackFrame<T>;
     end->data=temp->data;
     top=end;
        
        temp=temp->link;
        while(temp!=NULL){
            end->link=new StackFrame<T>;
            end=end->link;
            end->data=temp->data;
            temp=temp->link;
        }
        end->link=NULL;
    

    }


Read more here: https://stackoverflow.com/questions/68477825/how-to-implement-overload-operator-with-template-in-cpp

Content Attribution

This content was originally published by Rosemary Wu at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: