I wanted to pass an object - which is also a vector - through a function using pointers.
I believe the issue arises on line 75 (transaction(&user, userID);) where I attempt to pass the object "user" through to the function "transaction". I don't believe the classes are relevant so I left them out. Thank you so much in advance. Here is the source.cpp code:
void transaction(vector<Customer *> &user, int userID);
void newAccount(vector<Customer*> & user, int userID);
void intr(vector<Customer*> & user, int userID, int account, int interest);
void loans(vector<Customer*> & user, int userID, int account, int loan);
int main()
{
vector<Customer> user(3);
user[0].setname("Josh");
user[0].setID(1);
user[0].bank[0].setbalance(100);
user[0].bank[1].setbalance(101);
user[0].bank[0].setoverdraft(200);
user[0].bank[1].setoverdraft(201);
user[0].accounts = 1;
user[0].setpin(1202);
user[1].setname("John");
user[1].setID(2);
user[1].bank[0].setbalance(102);
user[1].bank[0].setoverdraft(202);
user[1].accounts = 0;
user[1].setpin(1203);
user[2].setname("Jack");
user[2].setID(3);
user[2].bank[0].setbalance(103);
user[2].bank[0].setoverdraft(203);
user[2].accounts = 0;
user[2].setpin(1204);
int input;
int userID;
int pin;
int account;
bool menu = true;
//Menu
while (menu)
{
cout << " - Enter '1' to display all customer names and ID's." << endl;
cout << " - Enter '2' for further transactions." << endl;
cout << " - Enter '3' to make a quick withdrawal of " << char(156) << "10 from an account." << endl;
cout << " - Enter '4' to exit." << endl;
cin >> input;
// List
if (input == 1)
{
for (int i = 0; i < user.size(); i++)
{
cout << "[Name: " << user[i].getname() << "\t" << "ID: " << user[i].getID() << "]" << endl;
}
cout << endl;
}
// Transactions
else if (input == 2)
{
cout << "Enter Customer ID: ";
cin >> userID;
cout << "Enter pin: ";
cin >> pin;
if (pin == user[userID].getpin())
{
transaction(&user, userID);
}
else { cout << "Pin invalid." << endl; }
}
// Quick withdrawal
else if (input == 3)
{
cout << "Enter Customer ID: ";
cin >> userID;
cout << "Enter pin: ";
cin >> pin;
cout << "Enter the account you wish to make a withdrawal from: ";
cin >> account;
if (pin == user[userID].getpin())
{
if (account <= user[userID].accounts)
{
if (user[userID].bank[account].getbalance() - 10 <= -user[userID].bank[account].getoverdraft())
{
user[userID].bank[account].withdraw(10);
}
else { cout << "Insignificunt funds. Overdraft limit (" << char(156) << user[userID].bank[account].getoverdraft() << ")" << endl; }
}
else { cout << "That account does not exist." << endl; }
}
else { cout << "Pin invalid." << endl; }
}
// Exit
else if (input == 4)
{
menu = false;
}
}
return 0;
}
void transaction(vector<Customer *> &user, int userID){...}
ERROR DESCRIPTION: initial value of reference to non-const must be an lvalue.
答案 0 :(得分:1)
您将错误的参数传递给transaction()
第一个参数引用客户指针的向量
std::vector<Customer*> &user
但是您传递了地址的客户
vector<Customer> user(3);
transaction(&user, userID);
您应该将vector<Customer>
更改为vector<Customer*> user(3)
。
或
改变void transaction(vector<Customer *> &user, int userID);
至void transaction(vector<Customer> &user, int userID);
如果你做同样的事情,其他功能也是如此。
关于错误,你确定这是问题吗?