过载=运算符,无法使其工作
Overload =operator, can't get it to work
我正在尝试在下面的代码中重载第 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
作为输入而不是Person
的operator==
。
其次,您尝试将运算符实现为一元运算符,这意味着它们必须是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 等获取数据)
相关文章:
- 布尔比较运算符是如何在C++中工作的
- 在 c++ 中对字符串进行操作的关系运算符无法按预期工作
- C++编程:运算符重载中的引用如何工作?
- 编译"运算符删除"时C++编译器如何工作?
- C++,()运算符重载,它的工作是什么
- 错误:三元运算符无法在对象中正常工作"cout"
- 为什么重载运算符"="动态数组的类上无法正常工作?C++
- 为什么在C++中使用关系运算符创建的模板函数不能对字符串正常工作?
- C++自定义删除运算符不能正常工作?
- 为什么我的模运算符无法正常工作?
- ostream_iterator运算符=在pair<int,int>上失败,但在包装器类上工作。我不能为成对<>重载运算符<<吗?
- 变量地址的运算符[]是如何工作的
- 为什么我的类工作正常,即使在返回垃圾值作为赋值运算符和空复制构造函数的返回之后
- 如何使用while循环找到GCD?模数运算符是如何工作的
- 让类与运算符一起工作更简单的方法
- 枚举运算符不工作
- 为什么运算符"new"需要指针才能工作?
- 有人可以解释速记赋值运算符的实际工作原理吗?
- 不工作 复制分配运算符
- 返回对象如何与分配运算符一起工作