"由于虚函数而无法声明抽象类型变量"尽管定义/实现了虚函数,但错误仍然存​​在

时间:2017-07-04 21:21:23

标签: c++ virtual-functions

我正在使用继承编写代码,使用名为BankAccount的基类和名为MoneyMarket Account的子类,并且我的代码收到以下错误

hw7main.cpp: In function ‘int main()’:
hw7main.cpp:9: error: cannot declare variable ‘account1’ to be of abstract type ‘MoneyMarketAccount’
hw7.h:36: note:   because the following virtual functions are pure within ‘MoneyMarketAccount’:
hw7.h:41: note:     virtual bool MoneyMarketAccount::withdraw(double)

基于其他类似问题的答案,我试图覆盖虚函数,退出,但我仍然得到同样的错误。

这是我的基类接口(文件名:hw7base.h):

#ifndef HW7BASE_H
#define HW7BASE_H
#include <iostream>
#include <string>
using namespace std;

class BankAccount
{
    public:
    //constructors
    BankAccount();

    //member functions
    bool deposit(double money);
    virtual bool withdraw (double money)=0;

    //accessor functions
    void getName(BankAccount* account);
    void getBalance(BankAccount* account);

    //transfer function
    //virtual void transfer (BankAccount* deposit, BankAccount* withdrawal, double money);
    //private:
    string name;
    double balance;
};


#endif

这是我的子类接口(文件名:hw7derived1.h):

#ifndef HW7DERIVED1_H
#define HW7DERIVED1_H
#include <iostream>
#include "hw7base.h"

using namespace std;

class MoneyMarketAccount: public BankAccount
{
    public:
        //constructor
        MoneyMarketAccount();
        //override function
        virtual bool withdraw(double money)=0;
        int number_of_withdrawals;

};

#endif

这是我的子类实现(文件名:hw7derived1.cpp):

#include "hw7derived1.h"
using namespace std;

//consturctor
MoneyMarketAccount::MoneyMarketAccount()
{
    number_of_withdrawals = 0;
}
bool MoneyMarketAccount::withdraw(double money)
{
    if (money<=0)
    {
        cout<<"Failed.\n";
        return false;
    }
    else
    {
        balance-=money;
            if(number_of_withdrawals>=2)
            {
                balance-=1.5;
                if (balance<0)
                {
                    balance=balance+1.5+money;
                    cout<<"Failed.\n";
                    return false;
                }
                else
                {
                    number_of_withdrawals++;
                    cout<<"Success.\n";
                    return true;
                }
            }
            else
            {       
                if (balance<0)
                {
                    balance+=money;
                    cout<<"Failed.\n";
                    return false;
                }
                else
                {
                    number_of_withdrawals++;
                    cout<<"Success.\n";
                    return true;
                }
            }
    }
}

感谢任何帮助/见解,谢谢!

1 个答案:

答案 0 :(得分:0)

此:

virtual bool withdraw(double money)=0;

(在MoneyMarketAccount - 派生类中)将函数声明为纯函数(就像它在基类中一样纯粹)。 即使你实现了它(所以可以被调用 - 是的,纯虚函数可以有实现,有时它确实有意义),这仍然使{{1} } abstract - 你不能实例化一个抽象类。

您可能希望将该函数声明为

MoneyMarketAccount
bool withdraw(double money) override; 中的

。这将使该类具体化,因为不再有任何纯(MoneyMarketAccount)函数,并且如果基类签名发生更改,编译器将来也会帮助您,因此您不再覆盖纯虚拟{{1}从它(=0位 - 这使得派生类中的冗余withdraw更加冗余。)