为什么我的程序有一个“向量下标超出范围”的实例?

时间:2013-12-09 01:42:33

标签: c++ sfml

Bassicly我的代码应该从int tt[]中的main()中获取内容

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[]读取零点后告诉程序崩溃,并提供错误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;
};

1 个答案:

答案 0 :(得分:2)

即使r和c被正确初始化为(我假设)4,一旦你循环到y = 4(即x正在递增)的点,你不会重置a,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);