简体   繁体   中英

Overloading operator = error

Im trying to create a stack but im receiving errors with overloading my = operator. The stack is of type template. Heres 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 will be appreciated. Thanks

Please try the below signature

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

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

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

I can only suppose that the operator should look as

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM