我正在尝试构建一个非常简单的通讯录。我创建了一个联系人类,而通讯录是一个简单的列表。我正在尝试构建一个函数,以允许用户将联系人添加到通讯录中。如果我将代码放在函数外部,它可以正常工作。但是,如果我把它放在函数里面,它就不能正常工作。我认为这是传递引用与传递值的问题,我没有正确地处理它。这是该函数的代码:
void add_contact(list<Contact> address_book)
{
//the local variables to be used to create a new Contact
string first_name, last_name, tel;
cout << "Enter the first name of your contact and press enter: ";
cin >> first_name;
cout << "Enter the last name of your contact and press enter: ";
cin >> last_name;
cout << "Enter the telephone number of your contact and press enter: ";
cin >> tel;
address_book.push_back(Contact(first_name, last_name, tel));
}
我没有收到任何错误提示,但当我尝试显示所有联系人时,只能看到原始联系人。
add_contact(addressBook)
时,list<Contact> addressBook
没有被更新。 - Adrian