473,406 Members | 2,954 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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.

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.

3 1420
weaknessforcats
9,208 Expert Mod 8TB
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
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
9,208 Expert Mod 8TB
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

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

Similar topics

5
by: john smith | last post by:
HI, when I try the following code, I get a segfault when compiled with VC.NET and g++ under cygwin. #1 vector<int> vi; #2 vector<int>::iterator ii = vi.begin(); #3 vi.reserve(10); #4 ...
4
by: Venn Syii | last post by:
I've searched all the forums but cannot find an answer to this question. I do the following: vector<MyClass*> myClassList; Later in the program I try to add to myClassList with a...
10
by: Bob | last post by:
Here's what I have: void miniVector<T>::insertOrder(miniVector<T>& v,const T& item) { int i, j; T target; vSize += 1; T newVector; newVector=new T;
98
by: jrefactors | last post by:
I heard people saying prefix increment is faster than postfix incerement, but I don't know what's the difference. They both are i = i+1. i++ ++i Please advise. thanks!!
11
by: eeykay | last post by:
Hello, I am facing a starnge problem while erasing the last member in a vector. I am using VC++ .NET 2002 complier. I have vector of CComPtr<..> (irrelevant here), and then I iterate over the...
6
by: happyvalley | last post by:
Hi, I want to remove some elements from a vector, the following code doesn't work, seems it doesn't allow me to remove an element when iterating the vector. (make sense), just wonder, how to do...
6
by: Xernoth | last post by:
Hi, I have an exercise that requests the following: Write a function that reads words from an input stream and stores them in a vector. Use that function both to write programs that count the...
4
by: Mizipzor | last post by:
During my coding Ive found two vector classes on the internet. Ive modified them both a little but the do both have advantages and disadvantages. vector1: http://rafb.net/p/4FVdh699.html...
2
weaknessforcats
by: weaknessforcats | last post by:
Handle Classes Handle classes, also called Envelope or Cheshire Cat classes, are part of the Bridge design pattern. The objective of the Bridge pattern is to separate the abstraction from the...
7
by: ademirzanetti | last post by:
Hi there !!! I would like to listen your opinions about inherit from a STL class like list. For example, do you think it is a good approach if I inherit from list to create something like...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.