不存在默认构造函数错误
No default constructor exists error
这是我的 .h 文件
#ifndef __cppProject__CheckingAccount__
#define __cppProject__CheckingAccount__
#include "Customer.h"
#include "Account.h"
#include <string>
using namespace std;
class CheckingAccount : public Account {
private:
int accountNumber; // 9nnnn - n randomly generated (0-9)
public:
/* The ctor sets the owner by passing it to the parent class. Randomly
* generates the account number which always starts with a '9' and is
* followed by four randomly generated numbers in the range 0-9.
*/
CheckingAccount(const Customer& owner);
int getAccountNumber() const;
/* Since the Account version is pure, this one will add the Account specific
* fields (dateOpened, owner) using the standard format we have been using.
* Refer to the screen capture for details.
*/
virtual string toString() const;
};
#endif /* defined(__cppProject__CheckingAccount__) */
这是我的 .cpp 文件
#include "Customer.h"
#include "CheckingAccount.h"
#include <string>
#include <sstream>
using namespace std;
CheckingAccount::CheckingAccount(const Customer& owner) : Account(owner){
this->accountNumber = 9999;
}
int CheckingAccount::getAccountNumber() const{
return accountNumber;
}
string CheckingAccount::toString() const {
stringstream o;
o << " CheckingAccount: {" << Account::toString() << ", accountNumber=" << accountNumber << ", owner=Customer: { "
<< " }";
return o.str();
}
我正在尝试在 main 中实例化一个 CheckingAccount,编译器正在强调我的客户 class 我正在传递 CheckingAccount 构造函数并告诉我没有提供默认构造函数。
如果我尝试在 CheckingAccount.cpp 文件中实现它时尝试创建一个无参数构造函数,它会给我同样的错误。
显然我遗漏了一些关于默认构造函数的基本信息,但我不确定它是什么。我不认为我必须有默认构造函数,除非 CheckingAccount 是父级 class,但事实并非如此。任何帮助将不胜感激。
编辑:
添加main,它很小,只有几个测试用例。
#include "Bank.h"
#include <string>
#include "CheckingAccount.h"
#include "Customer.h"
#include "Account.h"
using namespace std;
int Main() {
Bank bank;
Customer adam("Adam", "Apple");
Customer beatrice("Beatrice", "Bagel");
Customer chris("Chris", "Cucumber");
Customer temp;
CheckingAccount(adam);
return 0;
};
信不信由你:
CheckingAccount(adam);
与
相同
CheckingAccount adam;
但你想要的是
CheckingAccount adams_account(adam);
这是我的 .h 文件
#ifndef __cppProject__CheckingAccount__
#define __cppProject__CheckingAccount__
#include "Customer.h"
#include "Account.h"
#include <string>
using namespace std;
class CheckingAccount : public Account {
private:
int accountNumber; // 9nnnn - n randomly generated (0-9)
public:
/* The ctor sets the owner by passing it to the parent class. Randomly
* generates the account number which always starts with a '9' and is
* followed by four randomly generated numbers in the range 0-9.
*/
CheckingAccount(const Customer& owner);
int getAccountNumber() const;
/* Since the Account version is pure, this one will add the Account specific
* fields (dateOpened, owner) using the standard format we have been using.
* Refer to the screen capture for details.
*/
virtual string toString() const;
};
#endif /* defined(__cppProject__CheckingAccount__) */
这是我的 .cpp 文件
#include "Customer.h"
#include "CheckingAccount.h"
#include <string>
#include <sstream>
using namespace std;
CheckingAccount::CheckingAccount(const Customer& owner) : Account(owner){
this->accountNumber = 9999;
}
int CheckingAccount::getAccountNumber() const{
return accountNumber;
}
string CheckingAccount::toString() const {
stringstream o;
o << " CheckingAccount: {" << Account::toString() << ", accountNumber=" << accountNumber << ", owner=Customer: { "
<< " }";
return o.str();
}
我正在尝试在 main 中实例化一个 CheckingAccount,编译器正在强调我的客户 class 我正在传递 CheckingAccount 构造函数并告诉我没有提供默认构造函数。
如果我尝试在 CheckingAccount.cpp 文件中实现它时尝试创建一个无参数构造函数,它会给我同样的错误。
显然我遗漏了一些关于默认构造函数的基本信息,但我不确定它是什么。我不认为我必须有默认构造函数,除非 CheckingAccount 是父级 class,但事实并非如此。任何帮助将不胜感激。
编辑:
添加main,它很小,只有几个测试用例。
#include "Bank.h"
#include <string>
#include "CheckingAccount.h"
#include "Customer.h"
#include "Account.h"
using namespace std;
int Main() {
Bank bank;
Customer adam("Adam", "Apple");
Customer beatrice("Beatrice", "Bagel");
Customer chris("Chris", "Cucumber");
Customer temp;
CheckingAccount(adam);
return 0;
};
信不信由你:
CheckingAccount(adam);
与
相同CheckingAccount adam;
但你想要的是
CheckingAccount adams_account(adam);