|
| |||||||||||||||||||||||||||||||||||||||||||||
| cbouwkamp (83) | |
|
I need to create a vector of class objects. I know that is done similarly to this: class application{ string s; vector<account> accounts; for(;;){ (Imagine this is a loop and there is some code to pull the information out of a file until the end) accounts.push_back(account(s)); }; class account{ private: string a; account(string init_a): a(init_a); } is this the correct way of doing this and if so how do I output each vector assuming there is like 10? | |
|
Last edited on
|
|
| cbouwkamp (83) | |
| Anyone know how to construct class objects in vectors because I am getting desperate | |
|
|
|
| jsmith (5804) | |||
|
Well, this rather too pseudo-codish to be able to classify as right or wrong. Your for loop, for example, is not even inside any function. And your account constructor has an intializer list which is good, but is missing a function body. (It should take the parameter by const reference though). Output the vector by walking the elements in the vector one by one and printing them.
Be sure to write operator<< for account. | |||
|
|
|||
| cbouwkamp (83) | |
|
okay here is my code: application.h #include<fstream> #include<iostream> #include<string> #include<cstdlib> #include<vector> #include"account.h" #include"accountInfo.h" using namespace std; using namespace account_info; using namespace exception_handling; using account_class::account; #ifndef _APPLICATION_CLASS_ #define _APPLICATION_CLASS_ namespace application_class{ class application{ private: vector<account> accounts; string first; string last; string accountCode; long double balance; public: void accountInfo(istream& s, vector<account>& t){ try{ int count = 1; while(s.peek()==EOF){ accountCode = get_info(s, 10); first = get_info(s, 15); last = get_info(s, 25); s >> balance; if(s.peek()!='\n'){ throw exception_three(count); } s.ignore(); t.push_back(account(accountCode, first, last, balance)); ++count; } }catch(exception_three& three){ cout<< "Error 2203: "; three.errorStart();} } void run(){ try{ ifstream accountRead("account.dat"); if(!accountRead){ throw exception_one(); } ofstream report("report2.txt"); if(!report){ throw exception_two(); } accountInfo(accountRead, accounts); }catch(exception_one& one){cout<<"Error 2201: "<<one.what(); }catch(exception_two& two){cout<<"Error 2202: "<<two.what(); } } }; } #endif _APPLICATION_CLASS_ account.h #include<string> #include<iostream> #include<cstdlib> #include<vector> #include"exceptionHandling.h" using namespace std; using namespace exception_handling; #ifndef _ACCOUNT_CLASS_ #define _ACCOUNT_CLASS_ namespace account_class{ class account{ private: string account_code; string first_name; string last_name; double balance; public: account(string, string, string, double); }; } #endif _ACCOUNT_CLASS_ all the other header files are not necessary to figure out what I need. i need to know if I am pushing the application classes correctly and then I cannot figure how to overload operator<< and have it output each vector to a file in a nice fashion | |
|
|
|
| cbouwkamp (83) | |
| This has been driving me nuts for hours so any help at all? | |
|
|
|