二维向量return SIGSEGV。 SFML 游戏

Two dimensional vector return SIGSEGV. SFML Game

我刚写了一段代码,它从 .txt 文件加载 tilemap。一切都很好,我的 "game" 已编译。在此之后,我编写了负责在地图上添加像 House 这样的对象的代码。 现在代码编译,但是当我启动它时,出现 SIGSEGV 错误。我正在使用 SFML 2.4.2。这是怎么回事?调试器将我发送到 780 linetypes stl_vector.h 以及 Level.cpp:

tiles[i][j].setTexture(tekstury[tiles_atributes[i][j].type])

Level.h

class Object;

class Level
{
public:

    Level();
    Level(std::string filename);

    void loadFromFile(std::string filename, Object &object);        //load level from .txt

    ~Level(void);


    enum FieldType {
        GRASS,
        STONE,
        WATER,
        WATER_1,
        WATER_2,
        WATER_3,
        WATER_4,
        GRASS_BLOCK};

    struct Tile
    {
        FieldType type;
        bool isWall;
    };

    int width;
    int height;

    const static int tile_width = 40;       //width and height of single tile
    const static int tile_height = 40;

    sf::Texture tekstury[7];
    std::vector <std::vector < sf::Sprite > > tiles;
    std::vector < std::vector < Tile > > tiles_atributes;

};

class Object  
{
    public:

    int l;
    Object();
    ~Object();

    std::vector <sf::Texture> objects_textures;
    std::vector <sf::Sprite> objects_sprites;
    /*std::vector <std::vector<sf::Sprite>> additional_sprites;*/
};

Level.cpp

void Level::loadFromFile(std::string filename, Object &object)
{

    std::fstream file;
    file.open(filename, std::ios::in);      

    if (!file.is_open())
    {
        std::cout << "Not found " + filename;
        return;
    }


    file >> width >> height;


    tiles.resize(height);
    tiles_atributes.resize(height);
    for (int i = 0; i < height; i++)
    {
        tiles[i].resize(width);
        tiles_atributes[i].resize(width);
    }


    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < width; x++)
        {

            int tmp = 0;
            file >> tmp;

            tiles_atributes[y][x].type = FieldType(tmp);

            if(tmp==2 || tmp==3 ||
                    tmp==4 || tmp==5 ||
                    tmp==6)
                tiles_atributes[y][x].isWall = true;

            else
                tiles_atributes[y][x].isWall = false;
        }
    }

    int j=0;

    for (int i = 0; i <8; i++)
    {
        std::stringstream ss;
        ss<< "Img/Kafelki/tekstura" << i <<".png";
        std::string zrodlo;
        ss>>zrodlo;

        tekstury[i].loadFromFile(zrodlo);

        j++;
    }



    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            tiles[i][j].setTexture(tekstury[tiles_atributes[i][j].type]);
            tiles[i][j].setPosition(j*tile_width, i*tile_height);
        }
    }

    int m=0;
    int p=0;

    while (!file.eof())
    {

        std::string name;        // nazwa specjalnego Objectu
        file >> name;
        std::cout<< name;

        if (name == "[House]")
        {
            unsigned short x, y;
            file >> x >> y;
            m++;


            object.objects_sprites.resize(m);
            object.objects_sprites[p].setTexture(object.objects_textures[0]);
            object.objects_sprites[p].setPosition(x, y);

            int ob_height= object.objects_sprites[p].getGlobalBounds().height;
            int ob_width= object.objects_sprites[p].getGlobalBounds().width;

            int point_x=object.objects_sprites[p].getGlobalBounds().left;
            int point_y=object.objects_sprites[p].getGlobalBounds().top;

            for (int i = point_y/tile_height; i < ob_height/tile_height; i++)
            {
                for (int j = point_x/tile_width; j < ob_width/tile_width; j++)
                {
                    tiles_atributes[i][j].type = FieldType(7);
                }
            }

            for (int i = point_y/tile_height; i < ob_height/tile_height; i++)
            {
                for (int j = point_x/tile_width; j < ob_width/tile_width; j++)
                {
                    tiles[i][j].setTexture(tekstury[tiles_atributes[i][j].type]);
                }
            }



            p++;

        }

    }

    file.close();
}

您的数组 sf::Texture tekstury[7]; 中有 七个 纹理。但是您尝试加载 8 张图像,因为您有 八种 类型。当您尝试访问第八个纹理时......好吧。

你需要解决这个问题。停止在代码中使用幻数。使用一个知道您有多少种类型的常量。有些人以 COUNT 值结束他们的枚举......因为该值然后知道有多少,即使你在中间添加一些。但是一个简单的 const size_t FieldTypeCount = 8; 也可能有效。