我正在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