What exactly is an “assert”, or more specifically, how do I get rid of an error. When I create a vector of pointers to a class with data member int x, and then do this:
for(I=antiviral_data.begin();I<antiviral_data.end();I++)
{
if((*I)->x>maxx)
{
antiviral_data.erase(I);
}
}
And run the program, I get no errors until x is greater than maxx and I use .erase(), at which point I get this error:
Debug Assertion Failed!
Program: …My Documents\O.exe File:
…include\vector Line: 116Expression:
(“this->_Has_container()”,0)For information on how your program
can cause an assertion failure, see
the Visual C++ documentation on
asserts.(Press Retry to debug the application)
[Abort][Retry][Ignore]
Also, if I try to use cout:
cout<<(*antiviral_data.begin())->x<<endl;
I get this error:
Debug Assertion Failed!
Program: …My Documents\O.exe File:
…include\vector Line: 98Expression: vector iterator not
deferencableFor information on how your program
can cause an assertion failure, see
the Visual C++ documentation on
asserts.(Press Retry to debug the application)
[Abort][Retry][Ignore]
Could somebody please tell me why I can’t USE any of the data in the vector, and how to fix it?
ALSO: antiviral_data is a vector of pointers, with a single element:
antiviral_data.push_back(new aX1(player.x,player.y,'>'));
If that helps.
The most probable reason why you get the assertion is that you increment I after an erase. Try this instead:
See also http://www.cppreference.com/wiki/stl/vector/erase , search for invalid iterators on that page.