Original Post
Sorry if this has been posted before but I tried a few examples given to me and they are not working. Here is my code that I am using to dynamically create some objects, display them, then delete them. The CEntity class has a copy constructor and overloaded operator= implemented.
At deallocation, I get access violations and I am not sure how I would properly delete my objects. Anyone got any suggestions?
Also, while I am asking questions, is there a way to get the position int the vector the iterator is at?
#include <iostream>
#include <string>
#include <vector>
#include "Entity.h"
using namespace std;
int main(int argc, char* argv[])
{
vector<CEntity *> vEnt;
vector<CEntity *>::iterator it;
int entity;
string name;
cout<<"How many entities would you like?: ";
cin>>entity;
if(entity < 1 || entity > 255)
entity = 1;
cin.ignore(256, ''\n'');
CEntity *pObj;
for(int i=0; i<entity; ++i)
{
cout<<"Please enter a name for entity #"<<i+1<<": ";
getline(cin, name, ''\n'');
pObj = new CEntity(name);
vEnt.push_back(pObj);
}
for(it = vEnt.begin(); it != vEnt.end(); it++)
{
pObj = *it;
cout<<"Entity''s Name: "<<pObj->Name()<<endl;
}
//deallocation
it=vEnt.begin();
for(int i=0; it < vEnt.end(); i++)
{
delete vEnt[i];
}
return 0;
}