CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 36
  1. #1
    Join Date
    Mar 2017
    Posts
    105

    [RESOLVED] Debug Assertion Failed:vector subscript out of range

    I am experiencing a problem : 'Debug Assertion Failed : vector subscript out of range : line:1234'. I have used call stack to locate the line in my code with the error. The following is my code:-(i have commented the line with the error)

    Code:
    #include<SFML\Graphics.hpp>
    using namespace sf;
    
    int width = 1024;
    int height = 768;
    int roadW = 2000;
    int segL = 200;// segment length
    float camD = 0.84;// camera depth
    
    struct Line
    {
    	float x, y, z; // 3D center of line
    	float X, Y, W; // screen coord
    	float scale;
    
    	Line() { x = y = z = 0; }
    
    	// from world to screen coordinates
    	void project(int camX, int camY, int camZ)
    	{
    		scale = camD / (z - camZ);
    		X = (1 + scale*(x - camX)) * width / 2;
    		Y = (1 - scale*(y - camY)) * height / 2;
    		W = scale * roadW * width / 2;
    	}
    };
    
    void drawQuad(RenderWindow &w, Color c, int x1, int y1, int w1, int x2, int y2, int w2)
    {
    	ConvexShape shape(4);
    	shape.setFillColor(c);
    	shape.setPoint(0, Vector2f(x1 - w1, y1));
    	shape.setPoint(1, Vector2f(x2 - w2, y2));
    	shape.setPoint(2, Vector2f(x2 + w2, y2));
    	shape.setPoint(3, Vector2f(x1 + w1, y1));
    	w.draw(shape);
    }
    
    int main()
    {
    	RenderWindow app(VideoMode(width, height), "Outrun Racing!");
    	app.setFramerateLimit(60);
    
    	std::vector<Line> lines;
    
    	for (int i = 0; i < 1600; i++)
    	{
    		Line line;
    		line.z = i*segL;
    		lines.push_back(line);
    	}
    
    	int N = lines.size();
    
    	while (app.isOpen())
    	{
    		Event e;
    		while (app.pollEvent(e))
    		{
    			if (e.type == Event::Closed)
    				app.close();
    		}
    
    		app.clear();
    
    		///////draw road///////
    		for (int n = 0; n < 300; n++)
    		{
    			Line &l = lines[n%N];
    			l.project(0, 1500, 0);
    
    			Color grass = (n / 3) % 2 ? Color(16, 200, 16) : Color(0, 154, 0);
    			Color rumble = (n / 3) % 2 ? Color(255, 255, 255) : Color(0, 0, 0);
    			Color road = (n / 3) % 2 ? Color(107, 107, 107) : Color(105, 105, 105);
    
    			Line p = lines[(n - 1 % N)];// error somewhere here
    
    			drawQuad(app, grass, 0, p.Y, width, 0, l.Y, width);
    			drawQuad(app, rumble, p.X, p.Y, p.W*1.2, l.X, l.Y, l.W*1.2);
    			drawQuad(app, road, p.X, p.Y, p.W, l.X, l.Y, l.W);
    		}
    		app.display();
    	}
    
    	return 0;
    
    }
    Line 1234 in the file vector says that:-
    Code:
    	reference operator[](size_type _Pos)
    		{	// subscript mutable sequence
     #if _ITERATOR_DEBUG_LEVEL == 2
    		if (size() <= _Pos)
    			{	// report error
    			_DEBUG_ERROR("vector subscript out of range");
    			_SCL_SECURE_OUT_OF_RANGE;
    			}
    Please suggest a suitable fix.
    Last edited by VictorN; March 26th, 2017 at 02:44 AM. Reason: added Code tags

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Debug Assertion Failed:vector subscript out of range

    [moved from Visual C++ Bugs & Fixes forum]
    Victor Nijegorodov

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Debug Assertion Failed:vector subscript out of range

    Quote Originally Posted by A_Singh View Post
    Line 1234 in the file vector says that:-
    Code:
    	reference operator[](size_type _Pos)
    		{	// subscript mutable sequence
     #if _ITERATOR_DEBUG_LEVEL == 2
    		if (size() <= _Pos)
    			{	// report error
    			_DEBUG_ERROR("vector subscript out of range");
    			_SCL_SECURE_OUT_OF_RANGE;
    			}
    Please suggest a suitable fix.
    Well, the debugger shows you the error reason very clear: "vector subscript out of range"!
    So you are trying to use the vector index that is => the vector size!
    And it is what you have now to fix!
    Victor Nijegorodov

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Debug Assertion Failed:vector subscript out of range

    Code:
    Line p = lines[(n - 1 % N)];// error somewhere here
    % has higher precedence than - so this is
    Code:
    Line p = lines[n - (1 % N)];// error somewhere here
    when n is 0, 1 % N is greater than n so you are trying to access lines with a negative index!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Mar 2017
    Posts
    105

    Re: Debug Assertion Failed:vector subscript out of range

    Thank you all so much for your kind replays. But that didn't work for me.Im a bit new to visual c++. So if you could send me the line with the fix. It would be better.

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Debug Assertion Failed:vector subscript out of range

    Code:
    Line p = lines[(n - 1 % N)];// error somewhere here
    What are you trying to achieve? N is the number of elements in the vector lines (which from the code is 1600) and n varies between 0 and 299. I suspect you are trying to mean
    Code:
    Line p = lines[(n - 1) % N];// error somewhere here
    but when n is 0, n - 1 is negative so this is again a negative index into the vector which is the problem.

    Do you mean something like this
    Code:
    for (int n = 1; n < 300; n++)
    {
        Line &l = lines[(n - 1) % N];
        l.project(0, 1500, 0);
    
        Color grass = (n / 3) % 2 ? Color(16, 200, 16) : Color(0, 154, 0);
        Color rumble = (n / 3) % 2 ? Color(255, 255, 255) : Color(0, 0, 0);
        Color road = (n / 3) % 2 ? Color(107, 107, 107) : Color(105, 105, 105);
    
        Line p = lines[n % N];// error somewhere here
    
        drawQuad(app, grass, 0, p.Y, width, 0, l.Y, width);
        drawQuad(app, rumble, p.X, p.Y, p.W*1.2, l.X, l.Y, l.W*1.2);
        drawQuad(app, road, p.X, p.Y, p.W, l.X, l.Y, l.W);
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #7
    Join Date
    Mar 2017
    Posts
    105

    Re: Debug Assertion Failed:vector subscript out of range

    Thanks so much.I have completed my code.So could you please correct it in this code:-

    Code:
    #include<SFML\Graphics.hpp>
    using namespace sf;
    
    int width = 1024;
    int height = 768;
    int roadW = 2000;
    int segL = 200;// segment length
    float camD = 0.84;// camera depth
    
    struct Line
    {
    	float x, y, z; // 3D center of line
    	float X, Y, W; // screen coord
    	float scale, curve, spriteX, clip;
    	Sprite sprite;
    
    	Line() {curve = x = y = z = 0; }
    
    	// from world to screen coordinates
    	void project(int camX, int camY, int camZ)
    	{
    		scale = camD / (z - camZ);
    		X = (1 + scale*(x - camX)) * width / 2;
    		Y = (1 - scale*(y - camY)) * height / 2;
    		W = scale * roadW * width / 2;
    	}
    	void drawSprite(RenderWindow &app)
    	{
    		Sprite s = sprite;
    		int w = s.getTextureRect().width;
    		int h = s.getTextureRect().height;
    
    		float destX = X + scale * spriteX * width / 2;
    		float destY = Y + 4;
    		float destW = w * W / 266;
    		float destH = h * W / 266;
    
    		destX += destW * spriteX; // offsetX
    		destY += destH * (-1); // offsetY
    
    		float clipH = destY + destH - clip;
    		if (clipH >= destH) return;
    		s.setTextureRect(IntRect(0, 0, w, h - h*clipH / destH));
    		s.setScale(destW / w, destH / h);
    		s.setPosition(destX, destY);
    		app.draw(s);
    
    	}
    };
    
    void drawQuad(RenderWindow &w, Color c, int x1, int y1, int w1, int x2, int y2, int w2)
    {
    	ConvexShape shape(4);
    	shape.setFillColor(c);
    	shape.setPoint(0, Vector2f(x1 - w1, y1));
    	shape.setPoint(1, Vector2f(x2 - w2, y2));
    	shape.setPoint(2, Vector2f(x2 + w2, y2));
    	shape.setPoint(3, Vector2f(x1 + w1, y1));
    	w.draw(shape);
    }
    
    int main()
    {
    	RenderWindow app(VideoMode(width, height), "Outrun Racing!");
    	app.setFramerateLimit(60);
    
    	Texture bg;
    	bg.loadFromFile("bg.png");
    	bg.setRepeated(true);
    	Sprite sBackground(bg);
    
    	Texture t;
    	t.loadFromFile("tree.png");
    	Sprite sTree(t);
    
    	std::vector<Line> lines;
    
    	for (int i = 0; i < 1600; i++)
    	{
    		Line line;
    		line.z = i*segL;
    
    		if (i > 300 && i < 700) line.curve = 0.5;
    
    		if (i > 750) line.y = sin(i / 30.0) * 1500;
    
    		if (i % 20 == 0) { line.spriteX = -2.5; line.sprite = sTree;}
    
    		lines.push_back(line);
    	}
    
    	int N = lines.size();
    	int pos = 0;
    	int playerX = 0;
    
    	while (app.isOpen())
    	{
    		Event e;
    		while (app.pollEvent(e))
    		{
    			if (e.type == Event::Closed)
    				app.close();
    		}
    		if (Keyboard::isKeyPressed(Keyboard::Right)) playerX += 200;
    		if (Keyboard::isKeyPressed(Keyboard::Left)) playerX -= 200;
    		if (Keyboard::isKeyPressed(Keyboard::Up)) pos += 200;
    		if (Keyboard::isKeyPressed(Keyboard::Down)) pos -= 200;
    
    		while (pos >= N*segL) pos -= N*segL;
    		while (pos < 0) pos += N*segL;
    
    		app.clear();
    		int startPos = pos / segL;
    		int camH = 1500 + lines[startPos].y;
    		float x = 0, dx = 0;
    		int maxy = height;
    
    		///////draw road///////
    		for (int n = startPos; n <startPos+300; n++)
    		{
    			Line &l = lines[n%N];
    			l.project(playerX - x, camH, pos - (n>=N?N*segL:0));
    			x += dx;
    			dx + l.curve;
    
    			if (l.y >= maxy) continue;
    			maxy = l.y;
    
    			Color grass = (n / 3) % 2 ? Color(16, 200, 16) : Color(0, 154, 0);
    			Color rumble = (n / 3) % 2 ? Color(255, 255, 255) : Color(0, 0, 0);
    			Color road = (n / 3) % 2 ? Color(107, 107, 107) : Color(105, 105, 105);
    
    			Line p = lines[(n - 1 % N)]; // previous line
    
    			drawQuad(app, grass, 0, p.Y, width, 0, l.Y, width);
    			drawQuad(app, rumble, p.X, p.Y, p.W*1.2, l.X, l.Y, l.W*1.2);
    			drawQuad(app, road, p.X, p.Y, p.W, l.X, l.Y, l.W);
    		}
    
    		///////draw objects///////
    		for (int n = startPos + 300; n > startPos; n--)
    			lines[n%N].drawSprite(app);
    
    		app.display();
    	}
    
    	return 0;
    
    }
    Last edited by 2kaud; March 26th, 2017 at 11:10 AM. Reason: Added code tags

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Debug Assertion Failed:vector subscript out of range

    When posting code, please use code tags so that the code is readable. Go Advanced, select the formatted code and click '#'.

    This may or not be what you mean.

    Code:
    for (int n = startPos + 1; n <startPos+300; n++)
    ...
    Line p = lines[(n - 1) % N]; // previous line
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  9. #9
    Join Date
    Mar 2017
    Posts
    105

    Re: Debug Assertion Failed:vector subscript out of range

    Thank, I will check that and tell you.As i am new to this website, can anyone tell me how to use code tags?

  10. #10
    Join Date
    Mar 2017
    Posts
    105

    Re: Debug Assertion Failed:vector subscript out of range

    Thanks so much, It did work for me.

  11. #11
    Join Date
    Mar 2017
    Posts
    105

    Re: [RESOLVED] Debug Assertion Failed:vector subscript out of range

    But I can't get the desired results

  12. #12
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: [RESOLVED] Debug Assertion Failed:vector subscript out of range

    Quote Originally Posted by A_Singh View Post
    But I can't get the desired results
    What are the "desired results"?
    What are the results you get?
    What is the difference between them?
    Victor Nijegorodov

  13. #13
    Join Date
    Mar 2017
    Posts
    105

    Re: [RESOLVED] Debug Assertion Failed:vector subscript out of range

    The graphics which should appear with my coding, aren't coming,instead using the solution given by 2kaud, I am getting strange results.How can I send the picture of the desired results?, though I can send of what I am getting.

  14. #14
    Join Date
    Mar 2017
    Posts
    105

    Re: [RESOLVED] Debug Assertion Failed:vector subscript out of range

    I want that a road should appear with grass on the side, and I have saved special images for background(bg.png) and trees(tree.png) as I have mentioned in my code.

  15. #15
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: [RESOLVED] Debug Assertion Failed:vector subscript out of range

    Quote Originally Posted by A_Singh View Post
    How can I send the picture of the desired results?, though I can send of what I am getting.
    You can attach your pictures/images and other files to your post.
    Read the Announcement: Before you post....
    Victor Nijegorodov

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured