c++ - 如何使用指针通过函数传递对象?

标签 c++ function pointers object vector

我想通过一个使用指针的函数传递一个对象——它也是一个 vector 。

我认为问题出现在第 75 行(transaction(&user, userID);),我试图将对象“user”传递给函数“transaction”。 我不相信这些类(class)是相关的,所以我把它们排除在外。非常感谢你提前。这是 source.cpp 代码:

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){...}

错误描述:对非常量的引用的初始值必须是左值。

最佳答案

您向 transaction() 传递了错误的参数第一个参数采用对客户指针 vector 的引用

std::vector<Customer*> &user

但您正在传递一个客户 vector 地址

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);

如果你做同样的事情,其他功能也是如此。

关于错误,你确定是这个问题吗?

关于c++ - 如何使用指针通过函数传递对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40559960/

相关文章:

python 问答游戏错误

c++ - 在析构函数中删除指针

C 指针 - 非常基本的

c++ - 计算机如何将 float 转换为十进制字符串?

c++ - 为不同返回类型的函数返回函数指针的函数

javascript - 如何在不使用原型(prototype)的情况下链接函数?

python - 这个特定的 Python 函数组合背后的逻辑是什么?

c++ - 循环中前进迭代器

c++ - 模板化函数指针

c - 如何 'clear'一个字符串数组?