Hi,
Im working on a GUI project using Borland Builder 5 and I have a container class that is of type vector which has to hold objects of another class.
The objects to be stored are of Class Actor and ActorContainer is the class that should hold them.
When I add an Actor to the ActorContainer I get an EAccess Violation error thats highlights the "return size;" in my getSize() function within my Container class. As I am learning to program in GUI and am using a listbox to show all the actors in my container, I am not sure whether i am going wrong in my GUI or problem domain side of things.
Can someone suggest anything please that would make this work or an alternative way of doing it.
cheers
Code:
//this is the addActor button function in My GUI class
void __fastcall TForm2::AddActorClick(TObject *Sender)
{
Actor* newActor = new Actor;
AnsiString nameofActor = InputBox("Actor Name", "Please enter name for the Actor", "");
newActor->setActorName(nameofActor);
newActor->setPosition (theActor->getSize()+1);
//theActor is an object of ActorContainer to be used locally within my GUI class
theActor->addActor(newActor);
actors = new TStringList;
for (int i=0;i<theActor->getSize();i++)
{
AnsiString name;
name=theActor->getActor(i)->getPosition();
name=theActor->getActor(i)->getActorName();
actors->Add(name);
}
ListBox2->Items=actors;
}
------------------------------------------------------------------------
//This is the Actor Class
class Actor{
private:
AnsiString actorName;
int position;
public:
void setActorName(AnsiString s) {actorName=s;};
void setPosition(int p) {position=p;};
AnsiString getActorName() {return actorName;};
int getPosition() {return position;};
};
------------------------------------------------------------------------
//The ActorContainer Class
class ActorContainer{
private:
vector<Actor*>actorsNames;
int size;
public:
ActorContainer(){size=0;};
~ActorContainer();
void addActor(Actor* a)
{
actorsNames.push_back(a);
size++;
};
Actor* getActor(int i)
{
return actorsNames.at(i);
};
int getSize() {return size;};//this is where the Eaccess Violation points to
void removeActor(int i)
{
actorsNames.erase(actorsNames.begin()+i);
//does the list.erase function delete the object as well?
size--;
for(int i=0 ; i < size ; i++)
{
Actor* temp=actorsNames.at(i);
temp->setPosition(i+1);
}
};
};
p.s does the .erase() function for vectors delete objects from memory.