Правильный синтаксис реализации оператора присваивания (функции-члена) для шаблона

Вот файл .hpp:

template<typename T>
LinkedQueue<T> operator=(const LinkedQueue<T> & lhs, const LinkedQueue<T> & rhs)
{
m_data = rhs.m_data;
m_next = rhs.m_next;
}

Ошибка говорит о том, что первая строка должна быть нестатической функцией-членом. Вот класс, в котором это находится:

template<typename T>
class LinkedQueue:public AbstractQueue<T>
{
public:
T m_data;
LinkedQueue *m_next;

LinkedQueue<T> operator=(const LinkedQueue<T> & rhs);
LinkedQueue();
void clear();
void enqueue(T x);
void dequeue();
const T& front() const;
bool isEmpty() const;

};

Есть идеи, что за глупость я делаю неправильно?

1

Решение

Вы должны добавить классификатор класса в определение функции и удалить неиспользуемые lhs параметр:

template<typename T>
LinkedQueue<T>& LinkedQueue::operator=(const LinkedQueue<T> & rhs)
//            ^--- & should be added to the declaration, too
{
m_data = rhs.m_data;
m_next = rhs.m_next;
return *this;
}
3

Другие решения

вы должны написать так:

template<typename T>
class LinkedQueue:public AbstractQueue<T>
{
public:
T m_data;
LinkedQueue *m_next;

LinkedQueue<T> & operator=(const LinkedQueue<T> & rhs)
{
if (this != &rhs)
{
m_data = rhs.m_data;
m_next = rhs.m_next;
}

return *this;
}
LinkedQueue();
void clear();
void enqueue(T x);
void dequeue();
const T& front() const;
bool isEmpty() const;

};
1