This may be a simple answer, but after self teaching myself on vectors for a few hours of I haven't been able to find the answer. I'm using class objects in a vector, and trying to complete the delete functionality according to a class variable.
#Identify BUFFER 8
class CObject;
char* str;
char identifier[BUFFER] = "";
int j;
vector<CObject> group(0);
vector<CObject>::iterator itr = group.begin();
//code goes through some menu options. Some elements will be added to the vector
/***Erase functionality**/
str = getInput(); //function gets user input, returns char *
strcpy_s(identifier, BUFFER, str);
for(j = 0; j < group.size(); j++) //browses through the vector
{
if (strcmp(identifier, group[j].getID()) == 0)
break; //match was made,
}
if (j == fleet.size())
cout << "ID \"" << identifier << "\" not found." << endl;
else{
*itr = group.at(j); //***ERROR***
group.erase(itr);
}
This compiles, but when I run the program it crashes on the line specified, from the debugger it looks like itr doesn't initialize at all. The logic works, as I use the same loops to select specific vector items for display. But to erase an item in the vector I need an iterator, and that's what I'm having trouble getting. At first I thought it could be something with my class, but testing it out with a vector<int> I get the same problem. So how do I accomplish this? Am I missing something simple?