0

There is more code to this question in this previous question: C++ Trouble Inputting Data into Private Vector (invalid use)

I'm trying to output a vector of type "Account"

Account:

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

private:
    int depositAmount;
    int withdrawAmount;

public:
    static Account 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

This is the way I'm declaring the vector: std::vector<Account> accounts_;

And here's how I'm trying to print it to the screen:

for(int i=0; i < accounts_.size(); i++)
{     cout<< accounts_[i] <<endl;   }

But I'm getting this error "invalid operands to binary expression".

Current code;

class BankingSystem
{
int accountID;
char fileName;

private:
std::vector<Account> accounts_;

public:
void addAccount();
void storeAccount( Account );
void deleteAccount();
void accountInquiry();
void saveAccounts();
void loadAccountsFromFile();
friend class Account;
friend std::ostream& operator << (std::ostream&, const Account&);

}; // end of class BankingSystem
#endif


std::ostream& operator << (std::ostream& os, const Account& acc)
{
// output members to os
return os;
}

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;

}

//function gets data from createAccount
void BankingSystem::storeAccount( Account newAccountToAdd )
{
//append to vector "accounts_"
accounts_.push_back(newAccountToAdd);

}

void BankingSystem::deleteAccount()
{
cout << "\nEnter The Account ID: ";
cin >> accountID;


}

void BankingSystem::accountInquiry()
{
int n;
cout << "\n\t Enter The Account ID (-1 for all): ";
cin >> n;

//cout << accounts_.size();

if (n == -1)
{
    cout << "\n\t List of all Accounts; (" << accounts_.size() << ") TOTAL: ";

    for(int i=0; i < accounts_.size(); i++)
    {     
        cout<< accounts_[i] << endl;   
    }
}
else
{
    cout << "\n\t Listing Account: " << n;
    cout << "\n\t I should search the vector for the ID you input";
}
}
Community
  • 1
  • 1
frankV
  • 5,353
  • 8
  • 33
  • 46

2 Answers2

2

You need to provide the insertion operator:

std::ostream& operator<<( std::ostream& out, const Account& acct );

Then implement it internally by dumping each one of the fields with the appropriate format.

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
1

You should overload operator << for Account class. In class:

friend std::ostream& operator << (std::ostream&, const Account&);

In global (or yours, where Account is defined) namespace

std::ostream& operator << (std::ostream& os, const Account& acc)
{
    // output members to os
    return os;
}

or create some output function in class for example

void print(std::ostream& os) const { }

And then free-operator <<

std::ostream& operator << (std::ostream& os, const Account& acc)
{
    acc.print(os);
    return os;
}
ForEveR
  • 55,233
  • 2
  • 119
  • 133
  • Ok, your suggestion fixed the error but it's not outputting anything. Does my for loop really iterate through the entire vector and print out each element? – frankV Aug 10 '12 at 21:37
  • @frankV: Did you add any code to dump the contents of the `Account` object inside `operator<<`? You need to do it. – David Rodríguez - dribeas Aug 10 '12 at 21:55
  • If you're not getting anything then one possibility is that `accounts_.size()` is zero. Try adding a dummy `cout << "got here!\n";` inside your for loop. The other possibility is as David says. – jahhaj Aug 10 '12 at 21:55
  • I added `cout << accounts_.size();` and added an arbitrary amount of records into the vector. It'll print out the right number, just not the values. It's confusing. – frankV Aug 10 '12 at 22:00
  • @frankV Well you should post the code, both the code where you populate the vector and the code where you print it out. And of course the output that you do see. – jahhaj Aug 10 '12 at 22:05
  • @DavidRodríguez-dribeas `cout<< accounts_[i] << endl;` doesn't output each element? – frankV Aug 10 '12 at 22:05
  • @frankV: Not automatically, only if you've defined `operator<<` correctly. That was the point that David was making. – jahhaj Aug 10 '12 at 22:06
  • @jahhaj: it is in a previous question (which I believed you helped me with) http://stackoverflow.com/q/11908532/938865 the only difference is I ditched the custom structure and only use class Account as the vector type. – frankV Aug 10 '12 at 22:07
  • I don't see any definition of `operator<<` in the previous question. That seems to be the issue here. Could you post that code. – jahhaj Aug 10 '12 at 22:08
  • 1
    @frankV: See that comment `// output members to os`. That's exactly what you didn't do. You've defined an operator<< that does nothing, so not surprisingly when you use it you see nothing. – jahhaj Aug 10 '12 at 22:10
  • You have to add code to print out the values of `acc` that you want to see displayed. `os << whatever you want here;` – jahhaj Aug 10 '12 at 22:14
  • Like `os << accountID;` but they're in the Account class? How do I get them over to BankingSystem? Sorry, I'm obviously in over my head here. – frankV Aug 10 '12 at 22:16
  • First point, not `cout`. operator<< takes a stream parameter, use that stream. In the code above it is called `os`., so `os << ...`. – jahhaj Aug 10 '12 at 22:18
  • 1
    Second point, you need to add accessor methods to `Account`, so you can get the values you need. For instance `class Account { private: int accountID; public: int getAccountID() const { return accountID; } };` Then you use that accessor method to get the value of the account ID from the acc variable. So `os << acc.getAccountID();`. Would recommend you get a book on C++, this stuff would be explained in any decent book. – jahhaj Aug 10 '12 at 22:21
  • 1
    The other way to do it is to add an all in one print method to Account, you then call the print method from the operator<< function. This is what David suggested. – jahhaj Aug 10 '12 at 22:23
  • Sorry, it was what ForEveR suggested. – jahhaj Aug 10 '12 at 22:41
  • @jahhaj: I've implemented these changes, but it's still not outputting. Would you join me in chat? I'd greatly appreciate it! – frankV Aug 10 '12 at 22:45
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/15197/discussion-between-jahhaj-and-frankv) – jahhaj Aug 10 '12 at 22:46