鱼生富贵 2023-02-03 17:14 采纳率: 97.4%
浏览 33
已结题

报错一直显示EmptyAccountNumber未定义,我看来看去觉得没问题,如何解决?


#include<iostream>
#include<vector>
using namespace std;
 
class BankAccount {
public:
    friend class Bank;
    string GetName() const { return name; }
    int GetNumber() const { return number; }
    double GetBalance() const { return balance; }
    double GetRate() const { return rate; }
    bool GetActive() const { return active; }
    BankAccount():name("Test Account"),number(Bank::EmptyAccountNumber),balance(0),rate(0),active(false){}
    double saving_intrest() const;
    void show() const;
private:
    string name;
    int number;
    double balance;
    double rate;
    bool active;
    void setName(string _name);
    void deposit(double in);
    bool withdraw(double out);
    void close();
    BankAccount(string _name, int _number, double _balance, double _rate, bool _active) :name(_name), number(_number), balance(_balance), rate(_rate), active(_active) {}
};
 
void BankAccount::setName(string _name)
{
    name = _name;
}
void BankAccount::deposit(double in)
{
    balance += in;
}
bool BankAccount::withdraw(double out)
{
    if (out <= balance)
    {
        balance -= out;
        return true;
    }
    else
        return false;
}
void BankAccount::close()
{
    active = false;
}
double BankAccount::saving_intrest() const
{
    return balance * rate;
}
void BankAccount::show() const
{
    cout << "Name: " << GetName() << ", Number: " << GetNumber() << ", Balance: " << GetBalance() << ", Intrest Rate: " << GetRate() << endl;
}
 
class Bank {
private:
    vector<BankAccount> accounts;
    string name;
    static double rate;
    static int _number;
    int findAccount(int index);
public:
    friend class BankAccount;
    static const int EmptyAccountNumber;
    Bank(const string _name) { name = _name; }
    int registerAccount(string _name, double in);
    void closeAccount(int number);
    void imitate_in(int number, double in);
    void imitate_out(int number, double out);
    int activeAccount();
    double sumAccount();
    void show_activeAccount();
    static void setRate(double _rate);
    static double getRate() { return rate; }
    const BankAccount& getAccount(int number);
};
 
const int Bank::EmptyAccountNumber = -10000;
int Bank::findAccount(int index)
{
    for (int i = 0; i < accounts.size(); i++)
        if (accounts[i].number == index)
            return i;
    return -1;
}
int Bank::registerAccount(string _name, double in)
{
    BankAccount account(_name, _number++, in, rate,true);
    account.name = _name;
    account.balance += in;
    accounts.push_back(account);
    return _number - 1;
}
void Bank::closeAccount(int number)
{
    accounts[findAccount(number)].close();
}
void Bank::imitate_in(int number, double in)
{
    accounts[findAccount(number)].deposit(in);
}
void Bank::imitate_out(int number, double out)
{
    accounts[findAccount(number)].withdraw(out);
}
int Bank::activeAccount()
{
    int sum = 0;
    for (int i = 0; i < accounts.size(); i++)
        if (accounts[i].active == true)
            sum++;
    return sum;
}
double Bank::sumAccount()
{
    double sum = 0;
    for (int i = 0; i < accounts.size(); i++)
        if (accounts[i].active == true)
            sum += accounts[i].GetBalance();
    return sum;
}
void Bank::show_activeAccount()
{
    for (int i = 0; i < accounts.size(); i++)
        if (accounts[i].active == true)
            accounts[i].show();
    cout << "银行当前总储户数量--总的存款数额:" << activeAccount() << "--" << sumAccount() << endl;
}
void Bank::setRate(double _rate)
{
    rate = _rate;
}
double Bank::rate;
int Bank::_number = 1;
const BankAccount& Bank::getAccount(int number)
{
    return accounts[findAccount(number)];
}
 
int main()
{
    Bank bank("ICBC");
    bank.setRate(0.013);
 
    int id1 = bank.registerAccount("MaYun", 10000);
    int id2 = bank.registerAccount("MaHua", 13200);
    bank.show_activeAccount();
    
    cout << "\n*******************************************************\n";
    bank.setRate(0.015);
    int id3 = bank.registerAccount("Musk", 10270);
    int id4 = bank.registerAccount("Zhang", 19080);
    bank.show_activeAccount();
 
    cout << "\n*******************************************************\n";
    bank.closeAccount(id3);
    bank.getAccount(id3).show();
    bank.imitate_out(id4, 2000);
    bank.show_activeAccount();
    cout << "\n*******************************************************\n";
    int id5 = bank.registerAccount("Wang", 7790);
    bank.getAccount(id5).show();
    bank.imitate_in(id5, 10000);
 
    bank.getAccount(id5).show();
    cout << "interest= ";
    cout << bank.getAccount(id5).saving_intrest() << endl;
 
    bank.show_activeAccount();
 
    return 0;
}
 
报错一直显示EmptyAccountNumber未定义,我看来看去觉得没问题,不知道到底哪里有问题

  • 写回答

4条回答 默认 最新

  • 春休夏末 2023-02-03 18:07
    关注
    
    类定义顺序的问题:
        BankAccount类中使用了Bank类,因为你的类都是定义在同一个文件中的所以会从前到后的顺序查找类定义 在BankAccount构造函数中引用Rank类的时候 前面未找到Rank类定义,所以报错。
        一般这种情况是可以直接在BankAccount前面先声明一下Rank类就可以解决了
    

    img

    
      但是又因为你这里引用了Rank类的静态变量所以必须要先定义Rank类再定义BankAccount才可以,所以你这里需要将Rank类的定义和静态变量的初始化移动到BankAccount类定义的前面。
    
    

    img

    总结:
        1.将Rank类定义移动到RankAccount类定义前面
        2.移动后在Rank类定义前声明RankAccount类
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 2月18日
  • 已采纳回答 2月10日
  • 创建了问题 2月3日

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序