For a class I have, it uses a vector to keep track of objects that are "in" it. I've recently restructured my code, and it is now broken. It does not keep track of objects correctly.
Everything appears to be fine, and most of debug statements I added check out.
Code:
unsigned short int atom_id = 0, room_id = 0, person_id = 0, item_id = 0;
class atom
{
public:
unsigned short int id, type;
atom *loc;
vector<atom> contents; //This is the vector in question.
atom()
{
type = ATOM; id = atom_id++;
}
atom(const atom& A)
{
}
~atom(){}
friend bool operator==(const atom& lhs, const atom& rhs)
{
cout << "Testing("<<lhs.type<<','<<lhs.id<<")("<<rhs.type<<','<<rhs.id<<")\n";
if(lhs.type != rhs.type) {return false;}
return (lhs.id==rhs.id);
}
virtual void look(void){}
void entered(atom *A)
{
cout <<A->name<<" entering "<<name<<","<<id;
A->loc = this;
cout << contents.size();
contents.push_back(*A);
cout << A->name << contents.size() << contents[0].name;
}
void exitted(atom *A)
{
cout << A->name<<" exitting "<<name;
vector<atom>::iterator index;
for(index = contents.begin(); index <= contents.end(); index++)
{
if(*index == *A)
{contents.erase(index);}
}
}
};
class room: public atom
{
public:
room(void) : atom(*this)
{
type = ROOM; id = room_id++;
}
};
As you can see, I have 3 debug lines in entered. The first at the beginning tells you that the parameter is trying to enter the source object and gives its ID number. The second just lists the size of the contents vector before it is added to. The third at the end lists the name, new vector size, and the name again as referenced by the vector as per contents[0].
When I add an object to room by calling entered and passing the object to it, the debug statement at the end of entered, it does not read the name from the vector. It is as if there is no name there to be had.
Then, when I call enter and pass another object to it, it says again that there are 0 elements to contents before added then 1 after.
I am not adding or subtracting to/from the vector anywhere else, and exitted is not being called either.
How can the vector be spontaneously dropping elements?