C++ “operator = must be a non-static member”是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/871264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:41:11  来源:igfitidea点击:

What does "operator = must be a non-static member" mean?

c++classoperator-overloadingnon-static

提问by

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

我正在创建一个双链表,并重载了 operator= 以使列表中的另一个相等:

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 "}".

另外,如果有帮助,第 74 行是定义的最后一行,带有“}”。

采纳答案by v3.

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

正是它所说的:运算符重载必须是成员函数。(在类中声明)

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

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=....

此外,从 = 返回 LHS 可能是个好主意,这样你就可以链接它(比如a = b = c) - 所以让它 list<T>& list<T>::operator=....

回答by Johannes Schaub - litb

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:

将该运算符放在您的类定义中。它必须是会员,因为它operator=很特别,无论如何你将它写成非会员也不会得到什么。非会员运营商有两个重要的主要好处:

  • Implicit conversions of the right andthe left side of the operator invocation
  • No need to know about internals of the class. Function can be realized as non-member non-friend.
  • 运算符调用右侧左侧的隐式转换
  • 无需了解类的内部结构。功能可以实现为非会员非友。

For operator=, both is not usable. Assigning to a temporary result of a conversion does not make sense, and operator=will need access to internals in most cases. In addition, a special operator=is automatically provided by C++ if you don't provide one (the so-called copy-assignment operator). Making it possible to overload operator=as a non-member would have introduced additional complexity for apparently no practical gain, and so that isn't allowed.

对于operator=,两者都不可用。分配给转换的临时结果没有意义,并且operator=在大多数情况下需要访问内部结构。此外,operator=如果您不提供特殊功能(所谓的复制赋值运算符),C++ 会自动提供一个特殊功能。使operator=作为非成员超载成为可能会引入额外的复杂性,显然没有实际收益,因此这是不允许的。

So change your code so that it looks like this (this assumes the operator=is nota copy-assignment operator, but assigning from a list<T>to something else. This isn't clear from your question):

因此,改变你的代码,以便它看起来像这样(这个假定operator=不是一个拷贝赋值运算符,而是从分配list<T>到别的东西这不是从你的问题不清楚。):

class MyClass {
...
    template<class T>
    MyClass& operator=(const list<T>& lst)
    {
        clear();
        copy(lst);
        return *this;
    }
...
};

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 return voidall of a sudden.

operator=a 再次返回对自身的引用是非常标准的。我建议你坚持这种做法。对于程序员来说,它看起来很熟悉,如果它void突然返回,可能会引起意外。

回答by cletus

If you overload an operator as a member function, you should use this template:

如果将运算符重载为成员函数,则应使用此模板:

class A {
  A& operator=(const A& other) {
    if (this != &other) {
      ...
    }
    return *this;
  }
}

Three things to note:

需要注意的三件事:

  1. Check for self-assignment with the assignment operator (as above);
  2. The argument should be a const reference; and
  3. Return the result of the operation as a non-const reference where you return *this to allow chaining of operators.
  1. 使用赋值运算符检查自赋值(如上);
  2. 参数应该是一个常量引用;和
  3. 将操作的结果作为非常量引用返回,您返回 *this 以允许链接运算符。

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:

您还可以重载类外部的运算符。这与本示例无关,因为您不能使用赋值运算符来执行此操作,但值得注意的是,因为在许多情况下它优于成员函数。典型的形式是:

class A {
  friend const A& operator+(const A& a, const A& b);
  ...
}
const A& operator+(const A& a, const A& b) {
  A& ret = ...
  return ret;
}

This one returns a const reference so you can't do this:

这个返回一个常量引用,所以你不能这样做:

(a + b) = c

回答by Demi

From C++ Standard, "Binary Operators":

来自 C++ 标准,“二元运算符”:

"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).

它希望您在类中将其定义为成员,或使其成为静态方法(在这种情况下,它应该采用两个参数(对于 lval 和 rval)。