By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 1,784 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

How do you increment a vector for classes? C++

P: 3
I've been working on a project to make a ATM bank application but I've ran into a problem. I need to make an unlimited number of vectors of the class Bank I've created.

I can't seem to find out how. I tried push_back but it did not work for me.

I know that there are some variables and loops that aren't used properly. I believe that this is all the code I need to display to get my question resolved.

Expand|Select|Wrap|Line Numbers
  1. using namespace std;
  2.  
  3. int main()
  4. {
  5.     bool loop1 = true;
  6.     bool loop2 = true;
  7.     bool loop3 = true;
  8.     bool loop4 = true;
  9.     int i = 0;
  10.     int clientInt;
  11.  
  12.     while (loop1)
  13.     {
  14.         vector<Bank> clients(2);
  15.  
  16.         while (loop2)
  17.         {
  18.             while (loop3)
  19.             {
  20.                 clients[i].set_account(clients);
  21.  
  22.                 if (clients[i].get_account_setup() == false)
  23.                     break;
  24.  
  25.                 while (loop4)
  26.                 {
  27.                     clients[i].main_menu(clients, i);
  28.  
  29.                     if (clients[i].get_mm_response() == 4)
  30.                         loop4 = false; i++;
  31.                 }
  32.                 //cout << i << endl; // for debugging purposes
  33.  
  34.                 loop4 = true;
  35.             }
  36.  
  37.             loop2 = false;
  38.         }
  39.  
  40.         cout << endl << endl;
  41.         system("pause");
  42.         return 0;
  43.     }
  44. }
  45.  
  46.  
May 6 '16 #1

✓ answered by weaknessforcats

This should work:

Expand|Select|Wrap|Line Numbers
  1. vector<Bank> clients;
  2.  
  3. Bank b;
  4.  
  5. clients.push_back(b);
What happens when you do this?

Of course, this example presumes necessary constructors are available.

Share this Question
Share on Google+
3 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
This should work:

Expand|Select|Wrap|Line Numbers
  1. vector<Bank> clients;
  2.  
  3. Bank b;
  4.  
  5. clients.push_back(b);
What happens when you do this?

Of course, this example presumes necessary constructors are available.
May 6 '16 #2

P: 3
It worked for me. It did what I asked for, thanks!

But now I need to know why did it work? If that's not too much to ask. I didn't understand what the compiler was trying to tell me. I understood how push_back worked for other basic types of vectors but not for this one.
May 6 '16 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
I don't know what error you were getting but all C++ library objects, like vector, presume you have a well-behaved class. That is, all necessary constructors, destructors, operator overloads, and methods are in place.

Further, the type in the push_back() must be the same type used to create the vector.
May 7 '16 #4

Post your reply

Sign in to post your reply or Sign up for a free account.