Overload =运算符,无法使其工作

时间:2017-10-25 00:21:06

标签: c++ operator-overloading

我试图在下面的代码中第9行重载=运算符:

void searchContact(vector<Person> &people){
    string searchTerm;
    vector<Person>::iterator it;

    cout << endl;
    cout << "Enter search term: ";
    getline(cin, searchTerm);

    it = find(people.begin(), people.end(), searchTerm);

    if (it != people.end()){
        cout << "Element found in: " << *it << '\n';
    }else{
        cout << "Element not found\n";
    }
}

我的方法是:

  int data;

  Person& operator=(Person& a) { return a; }
  Person& operator=(int a) {
    data = a;
    return *this;
  }

我收到此错误:

class.cpp:129:30: error: ‘Person& operator=(Person&)’ must be a nonstatic member function
   Person& operator=(Person& a) { return a; }
                              ^
class.cpp:130:26: error: ‘Person& operator=(int)’ must be a nonstatic member function
   Person& operator=(int a) {

我的做法出了什么问题,或者我从一开始就做错了什么?

2 个答案:

答案 0 :(得分:0)

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class Person{

private:
     int data;

public:
    Person(int data)
    {
        this->data = data;
    }

    int getData()
    {
        return data;
    }

    friend bool operator==( const Person &lhs, const int rhs);
};
bool operator== ( const Person &lhs, const int rhs )
{
    return lhs.data == rhs;
}

void searchContact(std::vector<Person> &people){
    int searchTerm = 1;
    vector<Person>::iterator it;

    it = find(people.begin(), people.end(), searchTerm);

    if (it != people.end()){
        cout << "Element found in: " << it->getData() << '\n';
    }else{
        cout << "Element not found\n";
    }
}



int main(int argc, char **argv)
{
    std::vector<Person> list1 = {1, 2, 3, 4};
    std::vector<Person> list2 = {1, 2, 3, 5};
    std::vector<Person> list3 = {1, 3, 7, 6, 9, 5, 2, 4};

    searchContact(list1);
    searchContact(list2);
    searchContact(list3);
}

我不知道你想要什么。请显示您的完整代码并告诉您想要什么。

如果你想在向量中找到元素,你可以像这样写(不是字符串。如果你想要找到字符串,只需将int更改为string并使用cin或其他方式获取数据)

答案 1 :(得分:0)

首先,您正在重载错误的运算符。 std::find()使用operator==(比较)代替operator=(分配)。并且,鉴于您要将std::string传递给std::find(),您需要operator==作为输入std::string,而不是Person

其次,您正在尝试将运算符实现为一元运算符,这意味着它们必须是Person类的非静态成员。编译器抱怨他们不是。

第三,如果std::find()找到匹配项,那么您将*it传递给std::cout,因此您需要一个重载的operator<<,其输出为Person

尝试这样的事情:

class Person
{
public:
    ...

    bool operator==(const string &rhs) const
    {
        // compare members of *this to rhs as needed...
        return ...; // true or false
    }

    /* alternatively:
    friend bool operator==(const Person &lhs, const string &rhs)
    {
        // compare members of lhs to rhs as needed...
        return ...; // true or false
    }
    */

    friend ostream& operator<<(ostream &out, const Person &p)
    {
        // output p to out as needed...
        return out;
    }

    ...
};

然后您的搜索代码将起作用:

void searchContact(vector<Person> &people)
{
    cout << endl;
    cout << "Enter search term: ";

    string searchTerm;
    getline(cin, searchTerm);

    vector<Person>::iterator it = find(people.begin(), people.end(), searchTerm);    

    if (it != people.end()) {
        cout << "Element found in: " << *it << '\n';
    } else {
        cout << "Element not found\n";
    }
}