将类传递给函数,给出错误

Passing a class to a function, giving error

本文关键字:出错 错误 函数      更新时间:2023-10-16

BankDatabase bank是类对象:

class BankDatabase
{
private:
    Customer* customers[25];
    Account *accounts[25];
    int count;
public:
    BankDatabase();
    Account* getAccount(int accountNum);
    Customer* getCustomer(int accountNum);
    void display();
    void createAccount();
};
class Customer //CUSTOMER CLASS
{
private:
    string fullName;
    int accountNumber;
    string address;
    int phoneNumber;
public:
    Customer();
    Customer(string name, int acc, string add, int phone);
    int getAccountNum() const;
    string getName() const;
    string toString() const;
        /*bool Customer::operator> (Customer* a);*/
    };
    class Account //ACCOUNT CLASS
{
protected:
    int accountNumber;
    int PIN;
    double totalBalance;
public:
    Account();
    Account(int acc, int P, double bal);
    bool validatePIN(int pin);
    int getAccountNum() const;
    double getTotalBalance() const;
    virtual string toString() const;
    void credit(double amount);
    virtual bool debit(double amount);
    /*bool Account::operator> (Account* a);*/
};

函数原型为

void AccountAccess(class bank); //This might be error

在 Main 之前。 int main:

    int main()
{
    BankDatabase bank;
    int decision;
    bool goodInput;
    bool isDone = false;
    do
    {
        cout << "Welcome to the Bank of Cthulhu!" << endl;
        cout << endl;
        do
        {
            cout << "Please select an option from the main menu: " << endl;
            cout << endl;
            cout << "1) Create an account" << endl;
            cout << "2) Access your account" << endl;
            cout << "3) Exit" << endl;
            cout << endl;
            cin >> decision;
            if (decision == 1)
            {
                //bank.createAccount(); this works
                goodInput = true;
            }
            else if (decision == 2)
            {
                AccountAccess(bank);
                goodInput = true;
            }
            else if (decision == 3)
            {
                isDone = true;
                goodInput = true;
            }
            else
                goodInput = false;
        } while (!goodInput);
    } while (!isDone);
    return 0;
}

我正在投入银行的实际功能是

void AccountAccess(BankDatabase b)
{ //Function that allows the user to access their account
    int accInput;
    int pin;
    bool goodInput;
    do
    {
        cout << "Enter account number or press 1 to return to the main menu: ";
        cin >> accInput;
        if(b.getAccount(accInput) != 0)
        {
                goodInput = true;
                break;
        }
        else if (accInput == 0)
        {
            cout << "Returning to main menu..." << endl;
            cout << endl;
            goodInput = true;
            break;
        }
        else if (b.getAccount(accInput) == 0)
        {
            cout << "Account not found. Please try again." << endl;
            goodInput = false;
        }
    } while (!goodInput);
    return;
}

给我错误"'void AccountAccess(bank('无法将参数 1 从'BankDatabase'转换为'bank'

我已经尝试了几种变体,但我不确定如何解决这个问题,我知道这很简单。任何帮助将不胜感激。

这是一个

错误:

void AccountAccess(class bank); //This might be error

您已经声明了一个名为 AccountAccess 的函数,其参数应该是类 bank 的对象。

它应该是:

void AccountAccess(BankDatabase bank);

它声明了一个名为 AccountAccess 的函数,其参数应该是类 BankDatabase 的对象。

首先,您没有BankDatabasecopy constructor

函数 AccountAccess 声明为按值而不是按引用传递BankDatabase实例。因此,编译器希望传递实例的副本。为此,您需要在类中有一个复制构造函数,如

BankDatabase(const BankDatabase& rhs);

或者,您可以简单地使用引用参数声明和定义函数AccountAccess

void AccountAccess(BankDatabase& bank);
...
void AccountAccess(BankDatabase& b)
{
....

这样就不需要复制,你更有可能得到你真正想要的东西。但是,我也会在两个地方使用相同的参数名称。

相关文章: