[英]Why is my program having an instance where “Vector Subscript out of Range”?
基本上我的代碼應該從main()
int tt[]
中獲取int
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,
};
然后暴露該程序,以根據其順序為矢量提供坐標,並在給出#1的情況下為紋理提供坐標。 基於我的cout語句tiletype = tt[count];
我可以從int tt[]
讀取fith零后得知程序崩潰,並給出錯誤Vector Subscript out of Range
。
我認為問題屬於我的代碼的這一部分:
for(x = 1; x < c;++x)
{
for(y = 0; y < r;++y)
{
tiletype = tt[count];
cout <<tiletype<<", ";
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 <<endl<<"Row "<<row<<": "; y = 0;
}
這是其余的代碼。
#include <SFML/Graphics.hpp>
#include <iostream>
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& 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 <<endl<<"Row "<<row<<": ";
for(x = 1; x < c;++x)
{
for(y = 0; y < r;++y)
{
tiletype = tt[count];
cout <<tiletype<<", ";
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 <<endl<<"Row "<<row<<": "; y = 0;
}
return true;
}
private:
int row;
int count;
int y,x;
int a,b,d,e;
int tiletype;
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
{
states.transform *= getTransform();
states.texture = &tiletex;
target.draw(vertices, states);
}
sf::VertexArray vertices;
sf::Texture tiletex;
};
即使r和c正確地初始化為(我假設)4,一旦循環到y = 4
(即x遞增)的點,也不會重置a,b,d或e-這意味着在第五個循環中,您有a = 16
, b = 17
, d = 19
和e = 20
,但是隨后您仍然嘗試訪問vertices[a]
(以及b和d和e)。 由於在此之前將頂點的大小調整為(2 * 2 * 4)
或16,因此您可以訪問向量的范圍之外。 因此, vector subscript out of range
誤差。
為了容納足夠多的頂點,以便容納要在向量中放置的4 *個圖塊,您需要執行以下操作:
vertices.resize(4 * r * c);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.