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

时间:2015-11-05 03:35:11

标签: c++ function class

BankDatabase bank是类对象:

char

函数原型是

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);*/
};

在main之前。 int main:

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

和我正在实施的实际功能是

    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(bank)'无法将参数1从'BankDatabase'转换为'bank'

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

2 个答案:

答案 0 :(得分:2)

这是一个错误:

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

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

应该是:

void AccountAccess(BankDatabase bank);

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

答案 1 :(得分:-2)

首先,copy constructor没有BankDatabase

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

BankDatabase(const BankDatabase& rhs);

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

void AccountAccess(BankDatabase& bank);

...

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

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