assertions?

Hi
When I try to add something to my vector VC++ gives me an assertion error
Debug Assertion Failed!

Program:...strategy\Debug\strategy.exe
File:c\Program Files\microsoft visual studio 9.0\vc\include\vectors
Line:160

Expression:("this->_Has_container()",0)
...

I can't post the code becouse it has almost 1000 lines.
Could anybody tell me what could be causing errors or what is assertion.
I've read http://msdn.microsoft.com/en-us/library/ww5t02fa(VS.71).aspx but didn't understand much
An assertion is a debugging facility.
it generally looks like this
assert (expression that should be true for program to continue)

Simple silly Example:
1
2
3
4
5
6
7
8
9
10
11
#include <assert.h>

int main()
{
    int a = 3;

    assert(a > 5);



}


The assert on line 7 - says that at that point in the program - the variable a should be greater than 5.
If the variable a is greater than 5 (true) then the program will continue, if a is < 5 the epression will be false and the
program will be halted with an assertion error.
The program file and line within the file will usually be displayed for your
information.

Now if we look at the code for the vector include file - round by line 160
this is the code that we see:

1
2
3
4
5
6
7
8
9
	_Myt& operator+=(difference_type _Off)
		{	// increment by integer
		_SCL_SECURE_VALIDATE(this->_Has_container());
		_SCL_SECURE_VALIDATE_RANGE(
			_Myptr + _Off <= ((_Myvec *)(this->_Getmycont()))->_Mylast &&
			_Myptr + _Off >= ((_Myvec *)(this->_Getmycont()))->_Myfirst);
		_Myptr += _Off;
		return (*this);
		}


_Myt is an iterator. So this code is the += operator overload for an iterator.
So it looks as though you are trying to increment an ininitialised vector iterator.
That is to say that you have not set the iterator to point to a vector correctly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    vector<int> myvec; //vector variable

    myvec.push_back(3); //put some stuff in the vector
    myvec.push_back(6);
    myvec.push_back(5);
    myvec.push_back(9);
    myvec.push_back(21);

    vector<int>::iterator itr; //make an iterator

    //itr+=3; //ERROR -  atttempting to use iterator before it is initialised.

    itr=myvec.begin(); //CORRECT - iterator initialsed

    itr+=3; //OK 


So check your code.
Topic archived. No new replies allowed.