“operator = 必须是非静态成员”是什么意思? 意思是?

发布于 2024-07-20 15:58:34 字数 347 浏览 10 评论 0原文

我正在创建一个双链表,并且已经重载了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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

若言繁花未落 2024-07-27 15:58:34

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

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

另外,从 = 返回 LHS 可能是个好主意,这样你就可以链接它(如 a = b = c) - 所以让它
列表& 列表::operator=....

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

郁金香雨 2024-07-27 15:58:34

将该运算符放入您的类定义中。 它必须是一个成员,因为 operator= 很特殊,无论如何你都不会通过将其写为非成员来获得任何东西。 非成员运算符有两个重要的主要优点:

  • 运算符调用右侧左侧的隐式转换
  • 无需了解类的内部结构。 非会员非好友即可实现功能。

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

因此,请更改您的代码,使其看起来像这样(这假设 operator= 不是复制赋值运算符,而是从 list< /code> 到其他东西。这从你的问题中不清楚):

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

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:

  • Implicit conversions of the right and the 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.

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

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 void all of a sudden.

回忆躺在深渊里 2024-07-27 15:58:34

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

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

需要注意的三件事:

  1. 使用赋值运算符检查自赋值(如上所述);
  2. 参数应该是 const 引用; 并将
  3. 操作结果作为非常量引用返回,其中返回 *this 以允许链接运算符。

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

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

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

(a + b) = c

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.

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
穿越时光隧道 2024-07-27 15:58:34

根据 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文