0

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

anton.burger
  • 5,637
  • 32
  • 48
frankV
  • 5,353
  • 8
  • 33
  • 46
  • You probably should read about references and pointers. You are copying the objects all the time with your methods. – fdomig Aug 10 '12 at 20:09
  • I will, thanks. I'm still very new to C++. This assignment is as advanced as I've ever gone. – frankV Aug 10 '12 at 20:24

3 Answers3

3

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
AJG85
  • 15,849
  • 13
  • 42
  • 50
  • ok, I realized they were unnecessary now (used in framing). I've removed them but now the error is "no viable conversion from newAccount" my custom structure. Should I ditch this? – frankV Aug 10 '12 at 20:14
  • 1
    Don't change your design, just because you get an error. No code would ever get written if everyone worked like that. Think about why you added `newAccount` in the first place (can't see the reason myself but you must have had a reason). Fix the error first, and maybe tell us which line you get the error on – jahhaj Aug 10 '12 at 20:19
  • the "no viable conversion error" is on the `accounts_.push_back(a);` – frankV Aug 10 '12 at 20:20
  • Ok. The vector is of type "Account", a class, but I'm trying to store a custom structure "newAccount". That's my problem! – frankV Aug 10 '12 at 20:22
  • 1
    OK you have a vector of `Account`, and you are trying to add an `newAccount`. So, I think you were right, ditch `newAccount` use `Account` instead. – jahhaj Aug 10 '12 at 20:23
  • You should start thinking about constructors, none of your classes have them. For instance you could have a constructor for Account which would look very much like your createAccount function. Except it would construct an Account object, not return a newAccount object. Do that and I think you'll be on the right track. – jahhaj Aug 10 '12 at 20:23
  • Will do. Is it okay to post the finished code to Stack for help with refactoring? Thanks for all your help!!! – frankV Aug 10 '12 at 20:26
  • jahhaj has some good advice here. If you haven't yet pick up a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) ... I would design these classes loosely coupled to avoid the need for a friend declaration, provide a few overloaded constructors, etc. The `Account` class could easily be your data container eliminating the need for the `struct newAccount` as well. – AJG85 Aug 10 '12 at 20:28
0

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.

jahhaj
  • 3,021
  • 1
  • 15
  • 8
0

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.

Rocky Pulley
  • 22,531
  • 20
  • 68
  • 106