2015-02-08 70 views
1

我有以下问题:进程返回139(0x8B)分段故障

我有点新的C++,抬头看如何使多维数组没有大小的预定义,我发现了一些和尝试。我得到一个Process返回139(0x8B)作为答案。我查了一下我发现的一切,但它根本没有帮助我。

我的系统:Linux Mint的64位编辑:代码::块和克利翁

项目: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

如果您希望有人帮助您,请不要链接到外部尺寸。并尽量减少运行项目的要求。 (请记住,好的问题和答案应该可以帮助未来的其他人,为此你需要一个“不变”的内容,而不是链接到github页面,当有人试图找到相同的页面时,它可能会有1000个更改在4年的时间里回答) – 2015-02-08 18:10:21

+0

是的,你说得对,我应该知道的更好...... – blueBurningCoder 2015-02-08 18:13:48

回答

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就是一个例子。

+0

建议使用不同于“保留”的措辞,以避免与'reserve()'成员函数的作用混淆 – 2015-02-08 21:18:21