将类传递给函数,给出错误
Passing a class to a function, giving error
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
的对象。
首先,您没有BankDatabase
的copy constructor
。
函数 AccountAccess
声明为按值而不是按引用传递BankDatabase
实例。因此,编译器希望传递实例的副本。为此,您需要在类中有一个复制构造函数,如
BankDatabase(const BankDatabase& rhs);
或者,您可以简单地使用引用参数声明和定义函数AccountAccess
:
void AccountAccess(BankDatabase& bank);
...
void AccountAccess(BankDatabase& b)
{
....
这样就不需要复制,你更有可能得到你真正想要的东西。但是,我也会在两个地方使用相同的参数名称。
相关文章:
- 分段错误当我试图运行程序时出错
- .h 和.cpp文件分离时出错,但仅使用 .h 文件时没有错误.我做错了什么?
- 创建 OpenCV 非自由版本 v4.3 时出错,可折叠.cpp错误 C2039、2605
- 制作时出错,C++"db.c:2127:错误:在"||"标记之前应使用";"
- C++语法错误,编译器不会警告或 int v = func(&v) 出错;
- 在VS2015中访问类成员时运行时错误,但在Linux上未访问时出错
- 如何更改路径以修复错误"./main:加载共享库 libmkl_core.so 时出错?
- 为什么在定义类之前声明类的对象会在友元类中给出错误,而在友元函数中不会出错
- 安装 mply 库时出错.致命错误:gsl/gsl_sf.h:没有这样的文件或目录
- Rcpp/C++/R:比较日期时间矢量与日期时间时出错(错误:"运算符>"的不明确重载)
- 编译特征程序时出错:错误:'seq'不是'Eigen'的成员
- 可能的 GCC 链接器错误会导致将弱符号和局部符号链接在一起时出错
- 非标准语法;使用 '&' 创建指向成员的指针错误,将成员函数分配给向量时出错
- 运算符重载时出错(错误:"运算符<<不匹配(操作数类型为"std::basic_ostream<char>"和"const char [2]")
- 编译类 C++ 中的线程时出错(错误 xthread)
- 加载GLEW库时出错(错误LNK2001)
- 通过函数指针调用函数时出错(错误C2064)
- 编译core.obj时出错:错误LNK2001:未解析的外部符号_harmony_core_init
- 链接c++静态库与android ndk时出错(错误:无法识别文件格式)
- 使用GCC编译游戏时出错.(错误:将"Screen"的含义从"class Screen&q