Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 进程返回139(0x8B)分段错误_C++_Multidimensional Array_Vector_Sfml - Fatal编程技术网

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

C++ 进程返回139(0x8B)分段错误,c++,multidimensional-array,vector,sfml,C++,Multidimensional Array,Vector,Sfml,我遇到了以下问题: 我对C++有点陌生,查阅了如何在没有预先定义大小的情况下制作多维数组,我发现了一些东西并尝试了它。我得到一个返回139(0x8B)的进程作为答案。我查了所有我找到的关于它的东西,但它对我一点帮助都没有 我的系统:LinuxMint64位,编辑器:Code::Blocks和Clion 项目: 基本守则: #include <SFML/Graphics.hpp> #include <iostream> #include <vector> us

我遇到了以下问题:

<>我对C++有点陌生,查阅了如何在没有预先定义大小的情况下制作多维数组,我发现了一些东西并尝试了它。我得到一个返回139(0x8B)的进程作为答案。我查了所有我找到的关于它的东西,但它对我一点帮助都没有

我的系统:LinuxMint64位,编辑器:Code::Blocks和Clion

项目:

基本守则:

#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;
}
#包括
#包括
#包括
使用名称空间std;
使用名称空间sf;
/*
*平铺:背景没有平铺
*
*设置大小:
*@param x:x-稍后磁贴的位置
*@param y:y-稍后瓷砖的位置
*@param height2:瓷砖的高度
*@param width2:瓷砖的宽度
*(由于范围解析操作员因某种原因未工作,因此存在2)
*设置矩形的大小、位置和颜色
*
*
*拉瓦:
*@param renderWindow:绘制矩形的窗口
*绘制矩形
*/
类瓷砖{
私人:
内部locX、locY、高度、宽度;
//
矩形形状矩形;
公众:
无效设置大小(整数x、整数y、整数高度2、整数宽度2){
locX=x;
locY=y;
高度=高度2;
宽度=宽度2;
矩形设置尺寸(向量2f(高度、宽度));
矩形setFillColor(颜色:蓝色);
直接设置位置(矢量2F(locX,locY));
}
void drawTile(RenderWindow*RenderWindow){
渲染窗口->绘制(矩形);
}
};
/*
*迷宫:包括所有瓷砖,仅用于操纵特定瓷砖
*@param xSize:x方向将有多少块瓷砖
*@param-ySize:y方向会有多少块瓷砖
*
*绘图迷宫:
*@param renderWindow:Tile.drawTile方法需要在其上绘制
*绘制地图中的所有平铺
*/
班级迷宫{
私人:
int sizeX=-1,sizeY=-1;
矢量地图;
公众:
迷宫(int-xSize,int-ySize){
sizeX=xSize;
sizeY=ySize;
for(int i=0;i不能仅仅通过索引插入到
std::vector
中,需要调用相应的构造函数来分配固定的空间量

std::vector<int> array(4);
// array[0 ... 3] are now accessible.
std::向量数组(4); //现在可以访问阵列[0…3]。
所以你的声明应该是这样的:

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).
MAP=std::vector(xSize,std::vector(ySize));
//现在进入地图[i][j]
//它的基本内容是,MAP有xSize数量的元素,其中每个元素默认为std::vector(ySize)。

就是一个例子。

如果你希望有人帮助你,不要链接到外部尺寸。并尝试降低项目运行的要求。(记住,好的问题和答案应该在将来帮助其他人,为此,你需要一个“常数”内容,而不是指向github页面的链接,当有人在4年内试图找到相同的答案时,可能会有1000多个更改)是的,你是对的,我应该知道得更清楚…建议使用不同于“reserve”的措辞,以避免与
reserve()
成员函数的功能混淆