没有默认构造函数存在错误

时间:2015-02-21 00:54:51

标签: c++ constructor

这是我的.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,并且编译器在我的Customer类下划线我正在传递CheckingAccount构造函数并告诉我没有提供默认构造函数。

如果我尝试在CheckingAccount.cpp文件中实现它时尝试创建一个no arg构造函数,它会给我同样的错误。

显然我缺少一些关于默认构造函数的基本信息,但我不确定它是什么。我不认为我必须有一个默认的构造函数,除非CheckingAccount是父类,而不是。任何帮助将不胜感激。

编辑:

添加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;
};

1 个答案:

答案 0 :(得分:1)

信不信由你:

CheckingAccount(adam);

相同
CheckingAccount adam;

但你想要的是

CheckingAccount adams_account(adam);