Hello,
I've coded the following which works to a certain extent! I've managed to get the vectors working with int but not with my class...

#include <iostream>
#include <vector>

using namespace std;

class BankAccount
{
public:
//void setAccount(int num, double amount); taken out replaced by constructor
   BankAccount(int num, double amount);
   BankAccount();
int getBalance();
int getAccountNumber();
void deposit(double AnAmount);
void withdraw(double AnAmount);

private:
int accountNumber;
int Balance;

};

BankAccount::BankAccount()
{
   accountNumber = 1;
   Balance = 0;
}

BankAccount::BankAccount(int num, double amount)
{
accountNumber = num ;
Balance = amount ;
}
int BankAccount::getBalance()
{
   return Balance;
}

int BankAccount::getAccountNumber()
{
   return accountNumber;
}

void BankAccount::deposit(double AnAmount)
{
Balance += AnAmount;
}

void BankAccount::withdraw(double AnAmount)
{
Balance -= AnAmount;
}


int main()
{
 

   BankAccount b;

vector<BankAccount>b1(10);

cout << b1.size() << endl;

cout << b1.capacity();

b1.push_back(b);

cout << b1.capacity();

    
	cout <<"The vector contains" << endl;
    for(int i = 0; i < b1.size(); i++)
           cout<< b1[i] << endl;



   return 0;
}

the Error I receive upon compilation is:

C:\computer science programming\CPP2\excerise sheet 9\question 1\vector of bankAccounts\bankaccounts.cpp(74) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class BankAccount' (or there is no acceptable con
version)

You need to write an operator<< for BankAccount:

class BankAccount 
{ 
public: 
//void setAccount(int num, double amount); taken out replaced by constructor 
  BankAccount(int num, double amount); 
  BankAccount(); 
  int getBalance(); 
  int getAccountNumber();
  void deposit(double AnAmount); 
  void withdraw(double AnAmount);

  friend ostream& operator<< ( ostream& out, const BankAccount& b )
  {
    return out<< b.accountNumber <<": "<< b.Balance;
  }
private: 
  int accountNumber; 
  int Balance; 
};

I assume this is a class thing then? Since in theory your calling a print out for an element in the vector?

I coded this before I took the OOP approach to it... and it works....

include<iostream>
using  namespace std;    

#include<vector>

int main()
{
	vector <int> smallvect; 
    cout << "intital capacity "<< smallvect.capacity()
        << "  and size “ << smallvect.size() << endl;

	smallvect.push_back(6);
    cout << "After push_back(6) capacity "<< smallvect.capacity()
        << "  and size “ << smallvect.size() << endl;

	smallvect.push_back(8);
    cout << "After pushback (8) capacity "<< smallvect.capacity()
         << "  and size “ << smallvect.size() << endl;

	smallvect.push_back(10);
    cout << "After push_back(10) capacity "<< smallvect.capacity()
         << "  and size " << smallvect.size() << endl;
        
	cout <<"The vector contains" << endl;
    for(int i = 0; i < smallvect.size(); i++)
           cout<< smallvect[i] << endl;
    return 0 ;
}

I understand that you've overloaded the << operator when involked in the main it does it job based on what you've written. I just thought the vector was smiliar to an array.
Not sure why the OOP doesnt work but your code fixed it

>I coded this before I took the OOP approach to it... and it works....
You're using a vector of int in that code. int already has an operator<< defined for it in the standard library.

>Not sure why the OOP doesnt work
For "cout<< whatever" to work, an overloaded operator<< needs to be defined for it. All of the built in types and some standard classes have this done for you, but for any new classes you write, you have to write it yourself.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.