0

我有一个“帐户”类的向量。它对 BankingSystem 类是私有的。这是我如何定义它们的。

账户类:

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

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

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 中创建帐户

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);

}

除了将数据存储在向量中之外,一切正常。该行accounts_.push_back(a);有此错误;“在静态成员函数中无效使用成员 'accounts_'。”

4

3 回答 3

3

静态方法无权访问类实例(否this),因此在内部storeAccount并且addAccount该成员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
于 2012-08-10T20:11:23.870 回答
0

静态成员函数对成员变量没有任何特殊访问权限,例如accounts_.

addAccount并且storeAccount是静态成员函数。你一定是有原因的,但这是一个错误。删除该静态,您将删除此错误。我猜你会有一个不同的问题。如果是这样,请提出另一个问题并找出解决该问题的正确方法。

于 2012-08-10T20:13:46.587 回答
0

该方法是静态的,因此它没有“this”指针,因此它不知道您要访问哪个对象的 accounts_ 变量。此外,您将永远不会在 createAccount() 上看到该打印输出,因为它是在返回调用之后。

于 2012-08-10T20:14:06.837 回答