错误说明:
“错误:虚拟外部类定义”
有问题的Cpp成员:
virtual void Account::creditBalance(double plus)
{
if(plus > 0)
balance += plus;
else
cout << "Cannot credit negative.";
}
virtual void Account::debitBalance(double minus)
{
if(minus <= balance)
balance -= minus;
else
cout << "Debit amount exceeded account balance.";
}
其余代码在这里(虽然我不确定是否有必要):http://pastebin.com/de5e9f77
答案 0 :(得分:13)
简单地剥离方法定义的虚拟。仅在声明时才需要virtual
关键字。
示例:
class Account {
public:
virtual void creditBalance(double);
virtual void debitBalance(double);
};
void Account::creditBalance(double plus)
{
if(plus > 0)
balance += plus;
else
cout << "Cannot credit negative.";
}
void Account::debitBalance(double minus)
{
if(minus <= balance)
balance -= minus;
else
cout << "Debit amount exceeded account balance.";
}
答案 1 :(得分:3)
您只需要在类声明中使用virtual关键字,即在头文件中。因此,将您的cpp文件更改为:
void Account::creditBalance(double plus)
{
if(plus > 0)
balance += plus;
else
cout << "Cannot credit negative.";
}
void Account::debitBalance(double minus)
{
if(minus <= balance)
balance -= minus;
else
cout << "Debit amount exceeded account balance.";
}
答案 2 :(得分:1)
仅在声明中使用virtual关键字(在头文件中,在类定义中)而不是在实现中(通常是.cc文件)