Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将类传递给函数,给出错误_C++_Function_Class - Fatal编程技术网

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

C++ 将类传递给函数,给出错误,c++,function,class,C++,Function,Class,BankDatabase bank是类对象: class BankDatabase { private: Customer* customers[25]; Account *accounts[25]; int count; public: BankDatabase(); Account* getAccount(int accountNum); Customer* getCustomer(int accountNum); void displ

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
那是在主楼前面。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 AccountAccessbank”无法将参数1从“BankDatabase”转换为“bank”

我尝试过几种不同的方法,但我不确定如何解决这个问题,我知道这很简单。任何帮助都将不胜感激。

首先,您没有用于BankDatabase的复制构造函数

函数AccountAccess被声明为通过值而不是引用传递BankDatabase实例。因此,编译器希望传递实例的副本。要做到这一点,类中需要一个复制构造函数,如

BankDatabase(const BankDatabase& rhs);
或者,您可以使用引用参数简单地声明和定义函数AccountAccess:

void AccountAccess(BankDatabase& bank);

...

void AccountAccess(BankDatabase& b)
{
....
这样就不需要复制了,你就更有可能得到你真正想要的东西。但是,我也会在两个位置使用相同的参数名称。

这是一个错误:

void AccountAccess(class bank); //This might be error
您已经声明了一个名为AccountAccess的函数,其参数应该是bank类的对象

应该是:

void AccountAccess(BankDatabase bank);

声明了一个名为Access的函数,其参数应该是BooCub数据库的对象。< /P>这是C++吗?银行数据库的定义是什么?为什么要按值传递BankDatabase?请参阅以了解如何发布不起作用的代码。请提供一个。显示一个演示该问题的最小完整示例,可能会为您提供更好的帮助。但是您可以尝试将参数设置为引用BankDatabase&bank,并在声明和定义中使用相同的参数名称。在此之前是否有其他警告或错误消息?如果是这样,发布第一条这样的消息。有一个隐式生成的复制构造函数。这可能会在运行时导致错误行为,但不会生成编译error@M.M可能是这样,但很难确定默认的复制ctor是否符合要求,而且很少是。@Gread.and.power.Oz是的,他似乎在编写代码。原来那句话是不一样的,他把它编辑了。@M.M.我不知道你对我的仇恨。我第一次在这里发布我的输入代码,我被告知不要这样做,所以我停止了这样做,现在你基本上是告诉我发布我所有的代码。我已经发布了足够多的代码供其他人运行,并且我经常被告知尝试发布代码的不同方式,所以如果我是新手,请原谅,我只是按照过去人们告诉我的方式发布代码。不管怎样,我解决了这个问题。我在声明BankDatabase之前定义了函数原型,所以我只是在声明BankDatabase之后和main之前移动了它,它就工作了。@bankey您最初发布的消息是,您的原型是void AccountAccessBankDatabase bank;然后你编辑了你的帖子,说它是void AccountAccessclass bank;。很明显,第一个版本并不是你真正的代码。我的怨恨是,你编造东西而不是发布问题的实际代码,这是在浪费人们的时间。@M.M,因为我正在积极尝试通过你们所有人给我的输入使它工作。@bankey不,因为错误无法将参数1从“BankDatabase”转换为“bank”,这是因为行AccountAccessclass bank;无效。你一定从一开始就有这句话,没有告诉任何人。