“operator = 必须是非静态成员”是什么意思? 意思是?
我正在创建一个双链表,并且已经重载了operator=以使列表等于另一个:
template<class T>
void operator=(const list<T>& lst)
{
clear();
copy(lst);
return;
}
但是当我尝试编译时出现此错误:
container_def.h(74) : error C2801: 'operator =' must be a non-static member
另外,如果有帮助的话,第74行是最后一行的定义,用“}”。
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
Also, if it helps, line 74 is the last line of the definition, with the "}".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正如它所说:运算符重载必须是成员函数。 (在类内声明)
另外,从 = 返回 LHS 可能是个好主意,这样你就可以链接它(如
a = b = c
) - 所以让它列表& 列表::operator=....
Exactly what it says: operator overloads must be member functions. (declared inside the class)
Also, it's probably a good idea to return the LHS from = so you can chain it (like
a = b = c
) - so make itlist<T>& list<T>::operator=....
将该运算符放入您的类定义中。 它必须是一个成员,因为
operator=
很特殊,无论如何你都不会通过将其写为非成员来获得任何东西。 非成员运算符有两个重要的主要优点:对于
operator=
,两者都不可用。 分配给转换的临时结果没有意义,并且在大多数情况下operator=
将需要访问内部。 此外,如果您不提供特殊的operator=
(所谓的复制赋值运算符),C++ 会自动提供它。 作为非成员可以重载operator=
会带来额外的复杂性,显然没有实际收益,因此这是不允许的。因此,请更改您的代码,使其看起来像这样(这假设
operator=
不是复制赋值运算符,而是从list< /code> 到其他东西。这从你的问题中不清楚):
operator=
再次返回对其自身的引用是非常标准的。 我建议您坚持这种做法。 对于程序员来说,它看起来很熟悉,并且如果它突然返回void
,可能会引起意外。Put that operator inside your class definition. It must be a member because
operator=
is special and you would not gain something by writing it as a non-member anyway. A non-member operator has two important main benefits:For
operator=
, both is not usable. Assigning to a temporary result of a conversion does not make sense, andoperator=
will need access to internals in most cases. In addition, a specialoperator=
is automatically provided by C++ if you don't provide one (the so-called copy-assignment operator). Making it possible to overloadoperator=
as a non-member would have introduced additional complexity for apparently no practical gain, and so that isn't allowed.So change your code so that it looks like this (this assumes the
operator=
is not a copy-assignment operator, but assigning from alist<T>
to something else. This isn't clear from your question):It's pretty standard that a
operator=
returns a reference to itself again. I recommend you to adhere to that practice. It will look familiar to programmers and could cause surprises if it would returnvoid
all of a sudden.如果将运算符重载为成员函数,则应使用此模板:
需要注意的三件事:
您还可以重载类外部的运算符。 这与本示例无关,因为您无法使用赋值运算符来完成此操作,但值得注意的是,因为在许多情况下它优于成员函数。 典型的形式是:
这个返回一个 const 引用,所以你不能这样做:
If you overload an operator as a member function, you should use this template:
Three things to note:
You can also overload an operator external to the class. This isn't relevant to this example because you can't do it with the assignment operator but it's worth noting because in many cases it's superior to member functions. The typical form is:
This one returns a const reference so you can't do this:
根据 C++ 标准,“二元运算符”:
“二元运算符应由具有一个参数的非静态成员函数或具有两个参数的非成员函数来实现”
它希望您在类中将其定义为成员,或使其成为静态方法(在这种情况下,它应该采用两个参数(对于 lval 和 rval)。
From C++ Standard, "Binary Operators":
"A binary operator shall be implemented either by a non-static member function with one parameter or by a non-member function with two parameters"
It wants you to define this in a class, as a member, or make it a static method (in which case it should take two parameters (for both the lval and the rval).