简体   繁体   English

重载==函数

[英]Overloading the == function

I am currently working on creating an overloaded function for the == operator. 我目前正在为==运算符创建一个重载函数。 I am creating an hpp file for my linked list and I can't seem to get this operator working in the hpp file. 我正在为我的链表创建一个hpp文件,我似乎无法让这个操作符在hpp文件中工作。

I currently have this: 我目前有这个:

template <typename T_>
class sq_list 
{

bool operator == ( sq_list & lhs, sq_list & rhs) 
{
    return *lhs == *rhs;
};

reference operator * ()     {
        return _c;
    };

};
}

I get about 10 errors but they pretty much repeat as errors: 我得到大约10个错误,但它们几乎重复为错误:

C2804: binary 'operator ==' has too many parameters C2804:二进制'运算符=='参数太多
C2333:'sq_list::operator ==' : error in function declaration; C2333:'sq_list :: operator ==':函数声明中的错误; skipping function body 跳过功能体
C2143: syntax error : missing ';' C2143:语法错误:缺少';' before '*' 在'*'之前
C4430: missing type specifier - int assumed. C4430:缺少类型说明符 - 假定为int。 Note: C++ does not support default-int 注意:C ++不支持default-int

I've tried changing things around but I constanly get the same errors as above 我已经尝试过改变一些事情,但我一直得到与上面相同的错误

Any tips or assistance on this is greatly appreciated. 任何提示或帮助都非常感谢。

The member operator only has one argument, which is the other object. 成员运算符只有一个参数,这是另一个对象。 The first object is the instance itself: 第一个对象是实例本身:

template <typename T_>
class sq_list 
{
    bool operator == (sq_list & rhs) const // don't forget "const"!!
    {
        return *this == *rhs;  // doesn't actually work!
    }
};

This definition doesn't actually make sense, since it just calls itself recursively. 这个定义实际上没有意义,因为它只是递归地调用自身。 Instead, it should be calling some member operation, like return this->impl == rhs.impl; 相反,它应该调用一些成员操作,比如return this->impl == rhs.impl; .

You are declaring the == overload as part of the class definition, as a method instances will get. 您将==重载声明为类定义的一部分,因为方法实例将获得。 Thus, the first parameter you request, lhs , is already implicit: remember, within an instance's methods you have access to this . 因此,您请求的第一个参数lhs已经是隐含的:请记住,在实例的方法中,您可以访问this

class myClass {
    bool operator== (myClass& other) {
        // Returns whether this equals other
    }
}

The operator==() method as part of a class should be understood as "this object knows how to compare itself to others". 作为类的一部分的operator ==()方法应该被理解为“这个对象知道如何将自己与其他人进行比较”。

You can overload operator==() outside the class to receive two arguments, both objects being compared, if that makes more sense to you. 您可以在类外部重载operator ==()以接收两个参数,两个对象都要进行比较,如果这对您更有意义。 See here: http://www.learncpp.com/cpp-tutorial/94-overloading-the-comparison-operators/ 见这里: http//www.learncpp.com/cpp-tutorial/94-overloading-the-comparison-operators/

http://courses.cms.caltech.edu/cs11/material/cpp/donnie/cpp-ops.html http://courses.cms.caltech.edu/cs11/material/cpp/donnie/cpp-ops.html

The comparison operators are very simple. 比较运算符非常简单。 Define == first, using a function signature like this: 首先使用如下函数签名定义==:

  bool MyClass::operator==(const MyClass &other) const {
    ...  // Compare the values, and return a bool result.
  }

HOW to compare MyClass objects is all your own. 如何比较MyClass对象是你自己的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM