Joshua Talbot Joshua Talbot - 27 days ago 8
C++ Question

How do you pass an object through a function using a pointer?

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.

Answer

you are passing wrong arguments to the transaction() first parameter takes reference to vector of customer pointer

std::vector<Customer*> &user

but you are passing an address of vector of customer

vector<Customer> user(3);
transaction(&user, userID);

you should change
vector<Customer>
to
vector<Customer*> user(3).

or

change
void transaction(vector<Customer *> &user, int userID);
to
void transaction(vector<Customer> &user, int userID);

the same goes for other functions if you are doing the same thing.

about the error, are you sure this is the problem?