C++ Trouble Inputting Data into Private Vector (in

2019-03-05 05:57发布

问题:

I have a vector of class "Account". It's private to a class BankingSystem. Here's how I have them defined.

Account Class:

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

BankingSystem Class:

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

I'm trying to store new accounts in the vector in this manner.

1) addAccount function in BankingSystem.h

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) createAccount in 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) storeAccount in BankingSystem.h

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

}

Everything is working fine except storing data in the vector. The line accounts_.push_back(a); has this error; "invalid use of member 'accounts_' in static member function."

回答1:

A static method does not have access to a class instance (no this) so inside of storeAccount and addAccount the member accounts_ does not exist.

FYI: nothing after a return statement will be executed so the line cout << "\n\t Account ID: " << a.accountID << " added successfully."; is rather useless in your current code.

Consider the following implementation for reference:

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


回答2:

A static member function doesn't have any special access to member variables like accounts_.

addAccount and storeAccount are static member functions. You must have made them so for a reason but it was a mistake. Remove that static and you will remove this error. I'm guessing you'll then have a different problem. If so ask another question and find out the right way to solve that.



回答3:

The method is static, so it has no "this" pointer, so it has no idea what object you want to access the accounts_ variable of. Also, you will never see that printout on createAccount() because it's after the return call.



标签: c++ vector