我正在C++中练习OOP,并且正在创建一个银行帐户类。下面是类的定义:C++类实例声明无匹配函数
#include <iostream>
#include <cmath>
#include <limits>
#include <string>
#include <cstring>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
using namespace std;
const int acc_num_length = 8;
const int acc_id_num_length = 13;
class bankAccount {
private:
int acc_num; // account number
int acc_id_num; // account holder ID number
string acc_tel_no; // account holder telephone number
string acc_first_name; // account holder first name
string acc_last_name; // account holder last name
public:
/* constructor */
bankAccount() {
acc_num = 0;
acc_id_num = 0;
acc_tel_no = "";
acc_first_name = "";
acc_last_name = "";
}
/* detail retrieval */
int get_acc_num() {
return(acc_num);
}
int get_acc_id_num() {
return(acc_id_num);
}
string get_acc_tel_no() {
return(acc_tel_no);
}
string get_acc_first_name() {
return(acc_first_name);
}
string get_acc_last_name() {
return(acc_last_name);
}
};
现在在我的主程序我想声明这个类的一个实例,
int main() {
float acc_num = 0;
while (((cout << "Enter account number: ") && !(cin >> acc_num)) || (floor(acc_num) < acc_num)) {
cout << "Invalid account number.\n";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
int acc_num_int = acc_num;
string acc_num_string = to_string(acc_num_int);
while (acc_num_string.length() != acc_num_length) {
cout << "Invalid account number (8 characters only).\n";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cin >> acc_num;
cout << "Enter account number: \n";
}
bankAccount ba1 = bankAccount(acc_num_int,0, "","","");
bankAccount ba2 = bankAccount();
return 0;
}
现在一切正常,除了我的实例声明,
C:/Cartrack/C++/Creating a form/Form/main.cpp: In function 'int main()': C:/Cartrack/C++/Creating a form/Form/main.cpp:68:67: error: no matching function for call to 'bankAccount::bankAccount(int&, long long int, const char [1], const char [1], const char [1])'
bankAccount ba1 = bankAccount(acc_num_int,0, "","",""); ^C:/Cartrack/C++/Creating a form/Form/main.cpp:25:2: note: candidate: bankAccount::bankAccount() bankAccount() { ^~~~~~~~~~~ C:/Cartrack/C++/Creating a form/Form/main.cpp:25:2: note: candidate expects 0 arguments, 5 provided C:/Cartrack/C++/Creating a form/Form/main.cpp:16:7: note: candidate: bankAccount::bankAccount(const bankAccount&) class bankAccount { ^~~~~~~~~~~ C:/Cartrack/C++/Creating a form/Form/main.cpp:16:7: note: candidate expects 1 argument, 5 provided C:/Cartrack/C++/Creating a form/Form/main.cpp:16:7: note: candidate: bankAccount::bankAccount(bankAccount&&) C:/Cartrack/C++/Creating a form/Form/main.cpp:16:7: note: candidate expects 1 argument, 5 provided mingw32-make.exe[1]: * [Form.mk:97: Debug/main.cpp.o] Error 1 mingw32-make.exe[1]: Leaving directory 'C:/Cartrack/C++/Creating a form/Form' mingw32-make.exe: * [Makefile:5: All] Error 2 ====1 errors, 6 warnings====
我不确定为什么没有匹配的电话。我通过了两个整数和三个字符串,这似乎是大部分人读过现有威胁时所犯的错误。
任何人都可以帮忙吗? 谢谢!
Zane
你没有构造函数'的BankAccount(INT acc_num,诠释acc_id_num,串acc_tel_no,串acc_first_name,串acc_last_name)创建对象;' – Logman
没有构造借此'的BankAccount( acc_num_int,0,“”,“”,“”)' – Marcin
请不要像这样创建对象:'bankAccount ba1 = bankAccount(...)'。它工作,但是非常复杂。调试它以查明实际发生的情况。正确的语法是'bankAccount ba1(...)' – Rene