使用push_Back到我制作的类的向量时,Segfault 11

问题描述 投票:0回答:1

我正在开发游戏,并且有Tile类和Room类。在tile类中,我有一些基本组件[这里是整个标题]:

#include "SFML/Graphics.hpp"
#include <iostream>
using namespace std;

#pragma once

class Tile {
public:
    Tile(sf::Vector2f position, char type);

    ~Tile();

    // Functions

    void render(sf::RenderWindow& window);

    void update();

    // Variables

    sf::Vector2f position;
    sf::Vector2f size;

private:

    // Functions

    void updateSprite();

    void updateShape();

    // Variables


    sf::RectangleShape shape;
    sf::Texture texture;
    sf::Texture* p_texture;
    sf::Sprite sprite;

    char type;

};

这是Tile的源文件:

#include "Tile.hpp"
#include "Textures.hpp"

// Public
Tile::Tile(sf::Vector2f position, char type) {
    // TODO: print out the textures
    // so you can know if it actually
    // populates the vector
    Tile::position = position;
    Tile::type = type;
    switch(type) {
        case('#'): // Wall
            Textures::loadTexture("./Assets/Art/Tiles/basicTile4.png", p_texture);
            break;
        case(' '): // Basic tile
            Textures::loadTexture("./Assets/Art/Tiles/basicTile1.png", p_texture);
            break;
        case('$'):
            Textures::loadTexture("./Assets/Art/Tiles/basicTile2.png", p_texture);
            break;
        default: 
            cout << "DOING NOTHING; UNKNOWN SYMBOL IN TILE DECLARATION: " << type << endl;
    }
}

Tile::~Tile() {
}

void Tile::render(sf::RenderWindow& window) {

}

void Tile::update() {

}

// Private
void Tile::updateShape() {
    size = sf::Vector2f(sprite.getGlobalBounds().width, sprite.getGlobalBounds().height);
    shape.setSize(size);
    shape.setPosition(position);
}

void Tile::updateSprite() {
    sprite.setTexture(*p_texture);
    sprite.setPosition(position);
}

忽略我不释放p_texture成员,我在另一个类中对游戏中的所有纹理进行了此操作

在房间类中,我解析房间的文本并从中创建图块。

for(char i : roomLayout) {
        bool newLine = false;
        Tile *newTile = new Tile(currentPosition, i);
        switch(i) {
            case('*'): // Empty
                tiles.push_back(newTile);
                break;
            case('#'): // Wall
                tiles.push_back(newTile);
                break;
            case(' '): // Basic tile
                tiles.push_back(newTile);
                break;
            case('$'):
                tiles.push_back(newTile);
                break;
            case('\n'):
                newLine = true;
                break;
            default:
                cout << "DOING NOTHING; UNKNOWN SYMBOL IN ROOM DECLARATION: " << i << endl;
        }

        // If not a newline, then increase x
        if(!newLine)
            currentPosition.x += TILE_SIZE;
        else { // If a newline, then increase y, reset x
            currentPosition.y += TILE_SIZE;
            currentPosition.x = 0;
        }
    }

而且我正在尝试将每个图块添加到当前房间的tile向量中>

此外,在大多数情况下,请忽略同一行,在解决此问题后,我将对其进行更改

[[在房间标题中这样定义:]

std::vector<Tile*> tiles;

尝试将_push_back到Tile *向量时出现段错误。我最初没有使用Tile指针,而只是使用常规Tiles,但是导致了同样的问题。

我必须做一些愚蠢的事情,如果不是,那可能是我正在使用的图形库[SFML]的原因,因为它显然存在一些奇怪的问题[例如,使Texture变为静态会导致段错误]

谢谢

编辑1:

Textures :: loadTexture如下:

void Textures::loadTexture(string path, sf::Texture *texture) {
    if (!(texture->loadFromFile(path))) {
        cout << "ERROR << CANNOT FIND " << path << " << TEXTURES.CPP" << endl;
    }
    textures.push_back(texture);
    cout << "Textures size: " << textures.size() << endl;
}

我正在开发游戏,并且有Tile类和Room类。在tile类中,我有一些基本组件[这里是整个标题]:#include“ SFML / Graphics.hpp” #include ...

c++ pointers vector segmentation-fault sfml
1个回答
0
投票

感谢所有评论,我将确保将来更好地使用gdb,并从现在开始创建一个可重复的最小答案。

© www.soinside.com 2019 - 2024. All rights reserved.