什么是“operator =必须是非静态成员”意思? [英] What does "operator = must be a non-static member" mean?

查看:1611
本文介绍了什么是“operator =必须是非静态成员”意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个双向链表,并且重载了operator =以使列表等于另一个:

I'm in the process of creating a double-linked list, and have overloaded the operator= to make on list equal another:

template<class T>
void operator=(const list<T>& lst)
{
    clear();
    copy(lst);
    return;
}

但我得到这个错误,当我尝试编译:

but I get this error when I try to compile:

container_def.h(74) : error C2801: 'operator =' must be a non-static member

此外,如果有帮助,74行是定义的最后一行,带有}。

Also, if it helps, line 74 is the last line of the definition, with the "}".

推荐答案

它的意思:操作符重载与1参数必须是成员函数。 (在类中声明)

Exactly what it says: operator overloads with 1 parameter must be member functions. (declared inside the class)

template<class T>
void list<T>::operator=(const list<T>& rhs)
{
    ...
}

此外,可能是一个好主意,返回LHS从=所以你可以链接它(如 a = b = c ) - 所以使它
列表< T>

Also, it's probably a good idea to return the LHS from = so you can chain it (like a = b = c) - so make it list<T>& list<T>::operator=....

这篇关于什么是“operator =必须是非静态成员”意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆