无法将值分配给2D指针数组的元素分段故障

问题描述

编辑:发现错误。不要打扰。

在我的程序中,我需要在Room的{​​{1}}中初始化Dungeon。由于某种原因,我遇到了分段错误错误,但是测试项目中的完全相同的代码可以正确运行。

layout

还有其他项目:

// RPG.project

class Room {};

class Dungeon
{
public:
    Dungeon(const int n,const int m) // Dungeon layout is initialized
    {
        this->layout = new Room** [n]; 
        for ( unsigned int i = 0; i < n; i++ )
            layout[i] = new Room* [m];
      
        // All of the rooms are made nullptr as being non-existant at this point.                                                                                   
        for ( unsigned int i = 0; i < n; i++ )
        {
            for ( unsigned int j = 0; j < m; j++ )
            {
                layout[i][j] = nullptr;
            }
        }
    }

    Room*** getLayout() { return this->layout; }    

private:
    Room*** layout;
};

class Builder
{
private:
    Dungeon* dungeon;    

public:
    void buildroom(int x,int y) // ERROR here
    {
        Room*** temp = this->dungeon->getLayout();
        temp[x][y] = new Room(); // Segmentation fault error
    }
};

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)