gpt4 book ai didi

c++ - 将 C++ 函数设为虚函数时出错

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:07:54 25 4
gpt4 key购买 nike

错误状态:
“错误:虚拟外部类定义”

有问题的 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

最佳答案

简单地从方法定义中剥离虚拟。 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.";
}

关于c++ - 将 C++ 函数设为虚函数时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1862360/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com