C++ Trouble Inputting Data into Private Vector (invalid use)

前端 未结 3 451
梦如初夏
梦如初夏 2021-01-26 00:24

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          


        
3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-26 01:05

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

提交回复
热议问题