进程返回139(0x8B)分段错误

时间:2015-02-08 18:05:55

标签: c++ multidimensional-array vector sfml

我遇到了以下问题:

我对C ++有些新意,查看如何制作没有预定义大小的多维数组,我发现了一些东西并试了一下。我得到一个进程返回139(0x8B)作为答案。我查了一下我发现的一切,但它根本没有帮助我。

我的系统:Linux Mint 64位,编辑:Code :: Blocks和Clion

项目:https://github.com/blueburningcoder/AntWorld.git

基本代码:

#include <SFML/Graphics.hpp>
#include <iostream>
#include <vector>

using namespace std;
using namespace sf;


/*
 * Tile: the background is out of tiles
 *
 * setSize:
 * @param x: X-location of the Tile later on
 * @param y: Y-location of the Tile later on
 * @param height2: the height of the tile
 * @param width2: the width of the tile
 * (the 2 is there since the scope resolution operator didn't work for some reason)
 * setting the size, position and color of the rect
 *
 *
 * drawTile:
 * @param renderWindow: the window the rect is drawn in
 * drawing the rect
 */
class Tile {

    private:
        int locX, locY, height, width;
        //
        RectangleShape rect;

    public:
        void setSize(int x, int y, int height2, int width2){
            locX = x;
            locY = y;
            height = height2;
            width = width2;
            rect.setSize(Vector2f(height, width));
            rect.setFillColor(Color::Blue);
            rect.setPosition(Vector2f(locX, locY));
        }
        void drawTile(RenderWindow *renderWindow){
            renderWindow->draw(rect);
        }

};


/*
 * Maze: including all the Tiles, used to manipulate only specific Tiles
 * @param xSize: how many tiles are gonna be in x direction
 * @param ySize: how many tiles are gonna be in y direction
 *
 * drawMaze:
 * @param renderWindow: needed for the Tile.drawTile method to draw on
 * drawing all Tiles in the MAP
 */
class Maze {

    private:
        int sizeX = -1, sizeY = -1;
        vector<vector<Tile> > MAP;

    public:
        Maze(int xSize, int ySize){
            sizeX = xSize;
            sizeY = ySize;
            for(int i = 0; i < xSize; i++){
                for(int j = 0; j < ySize; j++){
                    Tile tile;
                    tile.setSize(i * 35, j * 35, 30, 30);
                    // tile might not have been initialized?
                    MAP[i][j] = tile;
                }
            }
        }
        void drawMaze(RenderWindow *renderWindow){
            // TODO: draw Tiles!
            for(int i = 0; i < sizeX; i++){
                for(int j = 0; j < sizeY; j++){
                    MAP[i][j].drawTile(renderWindow);
                }
            }
        }
};



int main()
{
    // Create the main window
    RenderWindow app(VideoMode(800, 600), "SFML window");

    // Load a sprite to display
    Texture texture;
    cout << "there's no error yet..." << endl;
    if (!texture.loadFromFile("cb.bmp")) {
        cout << "failed to load!" << endl;
        return EXIT_FAILURE;
    }
    Sprite sprite(texture);

    cout << "creating the maze ..." << endl;

    // Creating a 10 x 10 Maze
    Maze maze(10, 10);

    // Start the game loop
    while (app.isOpen())
    {
        // Process events
        sf::Event event;
        while (app.pollEvent(event))
        {
            // Close window : exit
            if (event.type == sf::Event::Closed)
                app.close();
        }

        cout << "gonna draw it ..." << endl;

        // Clear screen
        app.clear();

        // Draw the sprite
        app.draw(sprite);

        // drawing the Maze
        maze.drawMaze(&app);

        // Update the window
        app.display();
    }

    return EXIT_SUCCESS;
}

正确答案

1 个答案:

答案 0 :(得分:1)

您不能仅通过索引插入std::vector,您需要调用适当的构造函数来分配固定数量的空间。

std::vector<int> array(4);
// array[0 ... 3] are now accessible.

所以你的声明应该是这样的:

MAP = std::vector<std::vector<Tile>>(xSize, std::vector<Tile>(ySize));
// now access MAP[i][j] 
// It basically reads, MAP has xSize number of elements where each defaults to a std::vector<Tile>(ySize).

Here就是一个例子。