我正在嘗試構建一個非常簡單的通訊簿。我創建了一個Contact類,地址簿是一個簡單的列表。我正在嘗試構建一個允許用戶將聯繫人添加到地址簿的功能。如果我把我的代碼放在函數之外,它就可以正常工作。但是,如果我放入它,它不起作用。我相信這是一個通過引用與傳遞價值的問題,我並不認爲我應該這樣做。這是函數的代碼:C++通過列表作爲函數的參數
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(通訊錄)時,'名單通訊錄'是不是被更新;' –
Adrian
2012-02-15 22:48:15