Need to get() in a vector containing objects
Aug 23, 2011 at 9:37am UTC
Hi!
I need to use the Get() functions I wrote for a class. However I use a vector to create and store the objects of that class(need alot of them). So, I wonder how do I do this?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
#include "Map.h"
#include "global.cpp"
void Map::Create()
{
cout << "\n\nCreating the Map\n\n" ;
int a = 0;
while (a < NUM_CELLS)
{
m_Cells.push_back(a);
++a;
}
cout << "\n\nSuccesfully created the Map... puuh!\n\n" ;
cout << "It contains " << a << " cells\n\n" ;
return ;
}
void Map::Load()
{
cout << "\n\nLoading the Map settings...\n\n" ;
ifstream Mapfile;
string line;
int a=0;
Mapfile.open ("Cells.txt" );
if (Mapfile.fail()) // this checks if it loads right?
{
cout << "Failed load" ;
return ;
}
else {
vector<Cell>::iterator iter = m_Cells.begin();
while (getline(Mapfile,line))
{
stringstream(line) >> a;
iter->Setpop(a);
getline(Mapfile,line);
iter->Setbuildings(line);
++iter;
}
}
cout << "Load successful\n\n" ;
return ;
}
void Map::Save()
{
cout << "\n\nSaving the Map... This might take a while.\n\n" ;
ofstream Mapfile;
string line;
int a=0;
Mapfile.open("Cells.txt" );
if (Mapfile.fail()) //checks if it loads correctly
{
cout << "Failed Save" ;
return ;
}
else {
vector<Cell>::iterator iter = m_Cells.begin();
while (a < NUM_CELLS)
{
Mapfile << iter->Getpop() << endl;
Mapfile << iter->Getbuildings() << endl;
++a;
}
}
cout << "Save successful\n\n" ;
return ;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include "Cell.h"
class Map
{
public :
Map::Map(int spaces = 1) { m_Cells.reserve(spaces); }
// get
int Getpop(int );
string Getbuildings(int );
//operations
void Create();
void Save();
void Load();
void Access();
private :
vector<Cell> m_Cells;
};
any suggestions on improvement in the code are welcome.
Last edited on Aug 23, 2011 at 9:42am UTC
Topic archived. No new replies allowed.