Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is my program having an instance where "Vector Subscript out of Range"?
    primarykey
    data
    text
    <p>Bassicly my code is supposed to be taking ints from <code>int tt[]</code> in <code>main()</code></p> <pre><code>const int tt[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }; </code></pre> <p>The program then is exposed to give cordinates for a vector based on its sequential order and give cordinates for the texture if its gives #1. Based on my cout statement <code>tiletype = tt[count];</code> I can tell the program crashes after reading the fith zero from <code>int tt[]</code> and gives the error <code>Vector Subscript out of Range</code>.</p> <p>I assume the problem fall under this section of my code:</p> <pre><code> for(x = 1; x &lt; c;++x) { for(y = 0; y &lt; r;++y) { tiletype = tt[count]; cout &lt;&lt;tiletype&lt;&lt;", "; vertices[a].position = sf::Vector2f((0+y)*ts,ts*(x+1)+80); vertices[b].position = sf::Vector2f((0+y)*ts, (0+x)*ts+80); vertices[d].position = sf::Vector2f(ts+(y*ts),(0+x)*ts+80); vertices[e].position = sf::Vector2f(ts+(y*ts), ts*(x+1)+80); vertices[a].texCoords = mapcords(tiletype,0); vertices[b].texCoords = mapcords(tiletype,1); vertices[d].texCoords = mapcords(tiletype,2); vertices[e].texCoords = mapcords(tiletype,3); a += 4; b += 4; d += 4; e += 4;count++; } row += 1; cout &lt;&lt;endl&lt;&lt;"Row "&lt;&lt;row&lt;&lt;": "; y = 0; } </code></pre> <p>This is the rest of the code.</p> <pre><code>#include &lt;SFML/Graphics.hpp&gt; #include &lt;iostream&gt; using namespace std; using namespace sf; Vector2f mapcords(int tt,int corner); Vector2f mapcords(int tt,int corner) { if(tt ==0) { if (corner ==1) {return Vector2f(48,8 );} if(corner == 1) {return Vector2f(48,0);} if (corner ==2) {return Vector2f(56, 0);} if (corner == 3) {return sf::Vector2f(56, 8);} } else{ if(corner ==0) {return Vector2f(0,8 );} if(corner == 1) {return Vector2f(0,0);} if (corner ==2) {return Vector2f(8, 0);} if (corner == 3) {return sf::Vector2f(8, 8);} } return Vector2f(0,0 ); } class drawmap : public sf::Drawable, public sf::Transformable { public: bool load(const string&amp; tileset,const int* tt, int ts, int r, int c, int num) { count =0; row =1; a = 0; b = 1; d = 2; e = 3;y=0;x=0; tiletex.setRepeated(true); if (!tiletex.loadFromFile(tileset)) return false; tiletex.setRepeated(true); vertices.setPrimitiveType(sf::Quads); vertices.resize(2 * 2 * 4); cout &lt;&lt;endl&lt;&lt;"Row "&lt;&lt;row&lt;&lt;": "; for(x = 1; x &lt; c;++x) { for(y = 0; y &lt; r;++y) { tiletype = tt[count]; cout &lt;&lt;tiletype&lt;&lt;", "; vertices[a].position = sf::Vector2f((0+y)*ts,ts*(x+1)+80); vertices[b].position = sf::Vector2f((0+y)*ts, (0+x)*ts+80); vertices[d].position = sf::Vector2f(ts+(y*ts),(0+x)*ts+80); vertices[e].position = sf::Vector2f(ts+(y*ts), ts*(x+1)+80); vertices[a].texCoords = mapcords(tiletype,0); vertices[b].texCoords = mapcords(tiletype,1); vertices[d].texCoords = mapcords(tiletype,2); vertices[e].texCoords = mapcords(tiletype,3); a += 4; b += 4; d += 4; e += 4;count++; } row += 1; cout &lt;&lt;endl&lt;&lt;"Row "&lt;&lt;row&lt;&lt;": "; y = 0; } return true; } private: int row; int count; int y,x; int a,b,d,e; int tiletype; virtual void draw(sf::RenderTarget&amp; target, sf::RenderStates states) const { states.transform *= getTransform(); states.texture = &amp;tiletex; target.draw(vertices, states); } sf::VertexArray vertices; sf::Texture tiletex; }; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload