过载=运算符,无法使其工作

Overload =operator, can't get it to work

本文关键字:工作 运算符 过载      更新时间:2023-10-16

我正在尝试在下面的代码中重载第 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 foundn";
    }
}

我的方法是这样的:

  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) {

的方法有什么问题,还是我从一开始就做错了?

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

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

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

尝试这样的事情:

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 foundn";
    }
}
#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 foundn";
    }
}

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

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

如果你想在 vector 中查找元素,你可以这样写(不是字符串,如果你想找到字符串,只需将 int 更改为字符串并使用 CIN 等获取数据)