C++ 无法将数据输入私有(private) vector (无效使用)

标签 c++ vector

我有一个“账户”类的 vector 。它对 BankingSystem 类是私有(private)的。以下是我对它们的定义。

账户类别:

struct newAccount
{
string firstName;
string lastName;
string accountPass;
int accountID;
float accountBalance;

}; //end of structure newAccount

class Account
{
    string firstName;
    string lastName;
    string accountPass;
    int accountID;
    float accountBalance;

private:
    int depositAmount;
    int withdrawAmount;

public:
    static newAccount createAccount( int, float, string, string, string );  //creates new account
    void deposit( int );    //deposits money into account
    void withdraw(int);     //withdrawals money from account
    int retdeposit() const; //function to return balance amount
    friend class BankingSystem;

}; //end of class Account

银行系统类:

class BankingSystem
{
    int accountID;
    char fileName;

private:
    std::vector<Account> accounts_;

public:
    static void addAccount();
    static void storeAccount( newAccount );
    void deleteAccount();
    void accountInquiry();
    void saveAccounts();
    void loadAccountsFromFile();
    friend class Account;

}; // end of class BankingSystem

我正在尝试以这种方式在 vector 中存储新帐户。

1) BankingSystem.h中的addAccount函数

void BankingSystem::addAccount()
{
int ID;
float balance;
std::string pass, first, last;

cout << "\n\t Enter the Account ID: ";
cin >> ID;
cout << "\n\t Enter the passcode: ";
cin >> pass;
cout << "\n\t Enter Client's first name: ";
cin >> first;
cout << "\n\t Enter Client's last name: ";
cin >> last;
cout << "\n\t Enter starting balance: ";
cin >> setw(6) >> balance;

storeAccount( Account::createAccount( ID, balance, pass, first, last ) );

return;

}

2) Account.h中的createAccount

newAccount Account::createAccount( int ID, float balance, string first, string last, string pass )
{    
newAccount a;
a.accountID = ID;
a.accountBalance = balance;
a.firstName = first;
a.lastName = last;
a.accountPass = pass;

return a;

}

3) BankingSystem.h中的storeAccount

void BankingSystem::storeAccount( newAccount a )
{
accounts_.push_back(a);

}

除了在 vector 中存储数据外,一切正常。 accounts_.push_back(a); 行有这个错误; “在静态成员函数中无效使用成员‘accounts_’。”

最佳答案

静态方法不能访问类实例(没有this)所以在storeAccount里面和 addAccount成员(member)accounts_不存在。

仅供引用:return 语句之后不会执行任何内容,因此行 cout << "\n\t Account ID: " << a.accountID << " added successfully.";在您当前的代码中相当无用。

考虑以下实现以供引用:

using namespace std;

class Account
{
private: // data members
    string firstName;
    string lastName;
    string accountPass;
    int accountID;
    float accountBalance;

public:
    // constructor that initializes members
    Account(int id, float bal, const string& fname, const string& lname, const string& pass)
        : accountID(id), accountBalance(bal), firstName(fname), lastName(lname), accountPass(pass) {}

}; //end of class Account

class BankingSystem
{
private: // data members
    int accountID;
    char fileName;
    vector<Account> accounts_;

public:
    void addAccount()
    {
        int ID;
        float balance;
        string pass, first, last;

        // prompt input, initialize values, etc

            // construct a new Account from values and add it to vector
        accounts_.push_back(Account(ID, balance, first, last, pass));
    }
    void storeAccount( const Account& newAccount )
    {
            // add an already initialized account
        accounts_.push_back(newAccount);
    }


}; // end of class BankingSystem

关于C++ 无法将数据输入私有(private) vector (无效使用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11908532/

相关文章:

C# 将生成的数字列表转换为 int 数组

java - Java中如何按多个字段对对象进行排序?

c++ - 在二维 vector 中搜索数字序列的更快方法是什么?

c++ - 并发 std::vector 写入的效率

c++ - 为什么 std::thread 通过转发引用接受仿函数

C# - 从整数位掩码构造信号 Vector<T>

c++ - 对已经排序了 n 个第一个元素的 vector 进行排序?

c++ - VC++ 2010 中的 C++ Boost 安装问题(找不到文件)

c# - C++/C# 引用

python - scikit learn TSNE 转换应用于词向量时返回奇怪的结果