【问题标题】:Passing a class to a function, giving error将类传递给函数,给出错误
【发布时间】:2015-11-05 03:35:11
【问题描述】:

BankDatabase 银行是类对象:

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()
{
    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)' cannot convert argument 1 from 'BankDatabase' to 'bank'

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

【问题讨论】:

  • 这是 C++ 吗? BankDatabase的定义是什么?你为什么按值传递BankDatabase
  • 请参阅this page 了解如何发布不起作用的代码。
  • 请提供MCVE
  • 显示一个演示问题的最小完整示例,可能会为您提供更好的帮助。但您可以尝试将参数设为引用 (BankDatabase&amp; bank),并在声明和定义中使用相同的参数名称。
  • 在此之前是否还有其他警告或错误消息?如果是这样,请发布第一条此类消息。

标签: c++ function class


【解决方案1】:

这是一个错误:

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

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

应该是:

void AccountAccess(BankDatabase bank);

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

【讨论】:

  • @Gread.And.Powerful.Oz 是的,他似乎在编写代码。最初那行是不同的,他编辑了它。
  • @M.M 我不知道你对我的仇杀。我第一次在这里发帖时,我发布了我的输入代码,我被告知不要这样做,所以我停止这样做,现在你基本上是在告诉我发布我的所有代码。我已经发布了足够多的内容以供其他人运行,并且我经常被告知尝试发布代码的不同方式,所以如果我是新手并且只是发布人们过去告诉我发布的方式,请原谅我。无论如何,我解决了这个问题。我在声明 BankDatabase 之前定义了函数原型,所以我只是在声明它之后和 main 之前移动了它,它起作用了。
  • @bankey 你最初发布的原型是void AccountAccess(BankDatabase bank);,然后你编辑了你的帖子说它是void AccountAccess(class bank);。显然,第一个版本实际上并不是您的真实代码。我的仇杀是你编造东西而不是发布有问题的实际代码是在浪费人们的时间。
  • @M.M 因为我正在积极尝试通过你们所有人给我的输入来让它工作。
  • @bankey 否,因为错误“无法将参数 1 从 'BankDatabase' 转换为 'bank'”是因为 void AccountAccess(class bank); 行。你一定是从一开始就写了那句话,没有告诉任何人。
【解决方案2】:

首先,BankDatabase 没有 copy constructor

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

BankDatabase(const BankDatabase& rhs);

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

void AccountAccess(BankDatabase& bank);

...

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

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

【讨论】:

  • 有一个隐式生成的复制构造函数。 (这可能会在运行时导致不正确的行为,但不会产生编译错误)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-09
  • 1970-01-01
  • 1970-01-01
  • 2020-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多