2013-12-09 53 views
1

Bassicly我的代碼應該從int tt[]main()爲什麼我的程序有一個「Vector下標超出範圍」的實例?

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, 
    }; 

然後程序暴露給cordinates基於其順序在載體上,並給cordinates的紋理,如果它給人#1取整數。基於我的迴應聲明tiletype = tt[count];我可以告訴程序在從int tt[]讀取第零之後崩潰,並給出錯誤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; 
}; 
+0

請讓你擺脫一個字母變量名稱,它使代碼非常難以遵循! – yizzlez

回答

2

即使R和C正確初始化(我假設)4,一旦你循環到y = 4(即X是遞增)的時候,你不復位,b和d或e - 表示在第五個循環中,您有a = 16b = 17d = 19e = 20,但是您仍嘗試訪問vertices[a](以及b和d和e)。由於在此之前的頂點被調整爲(2 * 2 * 4)或16,因此您可以從其邊界訪問該矢量。因此,vector subscript out of range錯誤。

爲了保持足夠的頂點爲4 *要放在向量磚的數量,你將需要做的是這樣的:

vertices.resize(4 * r * c); 
+0

現在我想到了,如果你想讓每個tile有四個頂點(每個類型在tt中有一個tile),實際上你會想要更多的空間 - 可能像'vertices.resize( 4 * r * c);'。編輯答案... –