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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Broko668
    New Member
    • May 2016
    • 3

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

    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.

    Code:
    using namespace std;
    
    int main()
    {
    	bool loop1 = true;
    	bool loop2 = true;
    	bool loop3 = true;
    	bool loop4 = true;
    	int i = 0;
    	int clientInt;
    
    	while (loop1)
    	{
    		vector<Bank> clients(2);
    
    		while (loop2)
    		{
    			while (loop3)
    			{
    				clients[i].set_account(clients);
    
    				if (clients[i].get_account_setup() == false)
    					break;
    
    				while (loop4)
    				{
    					clients[i].main_menu(clients, i);
    
    					if (clients[i].get_mm_response() == 4)
    						loop4 = false; i++;
    				}
    				//cout << i << endl; // for debugging purposes
    
    				loop4 = true;
    			}
    
    			loop2 = false;
    		}
    
    		cout << endl << endl;
    		system("pause");
    		return 0;
    	}
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    This should work:

    Code:
    vector<Bank> clients;
    
    Bank b;
    
    clients.push_back(b);
    What happens when you do this?

    Of course, this example presumes necessary constructors are available.

    Comment

    • Broko668
      New Member
      • May 2016
      • 3

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

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        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.

        Comment

        Working...