#include<iostream> using namespace std; class Person { public: Person(string name, string phone, string email) : name_(name), phone_(phone), email_(email) {} string GetName() const { return name_; } string GetPhone() const { return phone_; } string GetEmail() const { return email_; } void SetPhone(string phone) { phone_ = phone; } void SetEmail(string email) { email_ = email; } private: string name_; string phone_; string email_; }; class AddressBook { public: AddressBook() {} void AddPerson(const Person& person) { person.push_back(person); } void RemovePerson(const string& name) { for (auto it = person.begin(); it != person.end(); ++it) { if (it->GetName() == name) { person.erase(it); break; } } } Person FindPerson(const string& name) const { for (auto& person : Person) { if (person.GetName() == name) { return person; } } return Person("", "", ""); } private: char <Person> person; }; int main() { AddressBook address_book; // 添加联系人 address_book.AddPerson(Person("张三", "13312345678", "zhangsan@qq.com")); address_book.AddPerson(Person("李四", "13987654321", "lisi@qq.com")); // 删除联系人 address_book.RemovePerson("李四"); // 查找联系人 Person person = address_book.FindPerson("张三"); if (person.GetName() != "") { cout << "姓名:" << person.GetName() << endl; cout << "电话:" << person.GetPhone() << endl; cout << "邮箱:" << person.GetEmail() << endl; } else { cout << "未找到该联系人!" << endl; } return 0; }以上代码错误,请修改正确
时间: 2024-04-28 10:21:48 浏览: 172
#include_iostream.h_
4星 · 用户满意度95%
以下是修改后的代码:
```cpp
#include<iostream>
#include<vector> // 添加头文件vector
using namespace std;
class Person {
public:
Person(string name, string phone, string email)
: name_(name), phone_(phone), email_(email) {}
string GetName() const { return name_; }
string GetPhone() const { return phone_; }
string GetEmail() const { return email_; }
void SetPhone(string phone) { phone_ = phone; }
void SetEmail(string email) { email_ = email; }
private:
string name_;
string phone_;
string email_;
};
class AddressBook {
public:
AddressBook() {}
void AddPerson(const Person& person) {
persons_.push_back(person); // 修改变量名person为persons_
}
void RemovePerson(const string& name) {
for (auto it = persons_.begin(); it != persons_.end(); ++it) {
if (it->GetName() == name) {
persons_.erase(it);
break;
}
}
}
Person FindPerson(const string& name) const {
for (auto& person : persons_) { // 修改变量名Person为persons_
if (person.GetName() == name) {
return person;
}
}
return Person("", "", "");
}
private:
vector<Person> persons_; // 修改变量类型char <Person>为vector<Person>
};
int main() {
AddressBook address_book;
// 添加联系人
address_book.AddPerson(Person("张三", "13312345678", "zhangsan@qq.com"));
address_book.AddPerson(Person("李四", "13987654321", "lisi@qq.com"));
// 删除联系人
address_book.RemovePerson("李四");
// 查找联系人
Person person = address_book.FindPerson("张三");
if (person.GetName() != "") {
cout << "姓名:" << person.GetName() << endl;
cout << "电话:" << person.GetPhone() << endl;
cout << "邮箱:" << person.GetEmail() << endl;
} else {
cout << "未找到该联系人!" << endl;
}
return 0;
}
```
主要修改:
- 添加头文件vector。
- 修改变量名person为persons_,并修改类型为vector<Person>。
- 修改遍历persons_时的变量名Person为person。
阅读全文