"cannot declare abstract type variable because of virtual function" 错误,尽管虚函数是 defined/implemented
"cannot declare abstract type variable because of virtual function" error despite the virtual function being defined/implemented
我正在使用继承编写代码,基 class 称为 BankAccount,子 class 称为 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)
根据其他类似问题的答案,我尝试重写虚函数,withdraw,但仍然出现同样的错误。
这是我的基础class界面(文件名: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
这是我的子class界面(文件名: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
这是我的子class实现(文件名: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;
}
}
}
}
任何 help/insight 不胜感激,谢谢!
这个:
virtual bool withdraw(double money)=0;
(In MoneyMarketAccount
- 派生的 class) 将函数声明为纯函数(就像它在基础 class 中是纯函数一样)。
即使你实现了它(所以它 can 被调用 - 是的,纯虚函数 can 有实现并且它有时确实有意义),这仍然使 MoneyMarketAccount
抽象 - 你不能实例化抽象 class.
您可能想将函数声明为
bool withdraw(double money) override;
在 MoneyMarketAccount
中。这将使 class 具体化,因为不再有任何纯 (=0
) 函数,并且如果基础 class 签名发生变化,编译器也会在将来帮助您,因此您不再从它覆盖纯虚拟 withdraw
(override
位 - 这使得派生的 class 中的冗余 virtual
额外冗余)。
我正在使用继承编写代码,基 class 称为 BankAccount,子 class 称为 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)
根据其他类似问题的答案,我尝试重写虚函数,withdraw,但仍然出现同样的错误。
这是我的基础class界面(文件名: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
这是我的子class界面(文件名: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
这是我的子class实现(文件名: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;
}
}
}
}
任何 help/insight 不胜感激,谢谢!
这个:
virtual bool withdraw(double money)=0;
(In MoneyMarketAccount
- 派生的 class) 将函数声明为纯函数(就像它在基础 class 中是纯函数一样)。
即使你实现了它(所以它 can 被调用 - 是的,纯虚函数 can 有实现并且它有时确实有意义),这仍然使 MoneyMarketAccount
抽象 - 你不能实例化抽象 class.
您可能想将函数声明为
bool withdraw(double money) override;
在 MoneyMarketAccount
中。这将使 class 具体化,因为不再有任何纯 (=0
) 函数,并且如果基础 class 签名发生变化,编译器也会在将来帮助您,因此您不再从它覆盖纯虚拟 withdraw
(override
位 - 这使得派生的 class 中的冗余 virtual
额外冗余)。