重载运算符=错误

Overloading operator = error

本文关键字:错误 运算符 重载      更新时间:2023-10-16

我试图创建一个堆栈,但由于重载my=运算符而收到错误。堆栈的类型为template。这是代码

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

任何帮助都将不胜感激。感谢

请尝试以下签名

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

尝试:

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

除了运算符的声明不正确之外,如果stack1是数组,则运算符体内似乎也存在标准算法副本的错误使用

我只能假设操作员应该看起来像

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