简体   繁体   English

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

[英]Why is my program having an instance where “Vector Subscript out of Range”?

Bassicly my code is supposed to be taking ints from int tt[] in main() 基本上我的代码应该从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,
    };

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. 然后暴露该程序,以根据其顺序为矢量提供坐标,并在给出#1的情况下为纹理提供坐标。 Based on my cout statement tiletype = tt[count]; 基于我的cout语句tiletype = tt[count]; I can tell the program crashes after reading the fith zero from int tt[] and gives the error Vector Subscript out of Range . 我可以从int tt[]读取fith零后得知程序崩溃,并给出错误Vector Subscript out of Range

I assume the problem fall under this section of my code: 我认为问题属于我的代码的这一部分:

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

This is the rest of the code. 这是其余的代码。

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

Even if r and c are properly initialized to (I assume) 4, once you've looped to the point where y = 4 (ie. x is incrementing) you don't reset a, b, d or e - meaning on the fifth loop through, you have a = 16 , b = 17 , d = 19 and e = 20 , but then you still try to access vertices[a] (and b and d and e). 即使r和c正确地初始化为(我假设)4,一旦循环到y = 4 (即x递增)的点,也不会重置a,b,d或e-这意味着在第五个循环中,您有a = 16b = 17d = 19e = 20 ,但是随后您仍然尝试访问vertices[a] (以及b和d和e)。 Since vertices just before this was resized to (2 * 2 * 4) , or 16, you then access the vector out of its bounds. 由于在此之前将顶点的大小调整为(2 * 2 * 4)或16,因此您可以访问向量的范围之外。 Hence, the vector subscript out of range error. 因此, vector subscript out of range误差。

In order to hold enough vertices for 4 * the number of tiles you want to put in the vector, you're going to need to do something like: 为了容纳足够多的顶点,以便容纳要在向量中放置的4 *个图块,您需要执行以下操作:

vertices.resize(4 * r * c);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM