Overload operator = error

I'm trying to create a stack but im getting errors when overloading my = operator. The stack has a type template. Here is the code

template <typename T>
T& ::stack& operator =(const stack& other)
{
    if (this == &other) return *this;

    copy(other.stack1[0], other.stack1[other.size], stack1[0]);
    return *this;
}

      

Any help would be appreciated. Thanks to

+3
c ++ stack templates


source to share


3 answers


Please try signatures



template <typename T>
stack<T>& stack<T>:: operator =(const stack<T>& other)

      

+3


source to share


Try:



template <typename T>
stack<T>& stack<T>::operator =(const stack& other)
{
    if (this == &other) return *this;

    copy(other.stack1[0], other.stack1[other.size], stack1[0]);
    return *this;
}

      

+2


source to share


Apart from the incorrect declaration of the operator, it seems that there is also an incorrect use of the standard copy of the algorithm inside the body of the operator, provided that stack1 is an array

I can only assume that the operator should look like

template <typename T>
stack<T> & stack<T>::operator =( const stack<T> &other )
{
    if ( this == &other ) return *this;

    this->size = other.size; 
    copy( other.stack1, other.stack1 + other.size, this->stack1 );

    return *this;
}

      

0


source to share







All Articles
Loading...
X
Show
Funny
Dev
Pics