Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.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++ 成员声明中的位置是否违反代码?_C++_Line - Fatal编程技术网

C++ 成员声明中的位置是否违反代码?

C++ 成员声明中的位置是否违反代码?,c++,line,C++,Line,刚才我问了一个问题,为什么下面的代码不起作用: std::vector<std::vector<std::vector<Tile_Base*>>> map_tile; // This is located in Map object. See below. int t_x, t_y; t_x = t_y = 200; map_tiles.begin(); // clear(), resize() and every other function still c

刚才我问了一个问题,为什么下面的代码不起作用:

std::vector<std::vector<std::vector<Tile_Base*>>> map_tile; // This is located in Map object. See below.
int t_x, t_y;
t_x = t_y = 200;
map_tiles.begin(); // clear(), resize() and every other function still causes problems
位于xutility。它说在读取内存的访问上有一个漏洞。我想也许我一路上找不到会员了?(使用VS'watch,我发现内存没有损坏)

因此,我对代码进行了修改,试图找出可能出现的错误,过了一段时间,我将map_tiles对象向下移动到列表的底部,它成功了:

// WORKS
class Map {
    std::vector<Tile_Base*> spawn_tiles;
    // map tile specific
    bool Is_Valid(int,int);
    std::string name;
    std::vector<std::vector<std::vector<Tile_Base*> > > map_tiles;
public:
// ...
}
// DOESN'T WORK
class Map {
    std::vector<std::vector<std::vector<Tile_Base*> > > map_tiles;
    std::vector<Tile_Base*> spawn_tiles;
    // map tile specific
    bool Is_Valid(int,int);
    std::string name;

public:

// ...
}
//有效
类图{
std::向量生成块;
//特定贴图块
bool是有效的(int,int);
std::字符串名;
std::向量映射图;
公众:
// ...
}
//不起作用
类图{
std::向量映射图;
std::向量生成块;
//特定贴图块
bool是有效的(int,int);
std::字符串名;
公众:
// ...
}
有人帮我指出哪里出了问题吗?我想不出任何合理的解释。

向量由两组离散的数据组成:内部状态和Ts数组。内部状态-容量、大小、指针-与数组分开。您描述的问题通常是由覆盖向量对象的内容引起的,即内部状态。要轻松查找此问题,可以使用容器类:

typedef std::vector<std::vector<std::vector<Tile_Base*> > > maptiles_t;

class CMapTiles
{
    unsigned int m_guard;
    maptiles_t m_tiles;
    enum { Guard = 0xdeadbeef };
public:
    CMapTiles() : m_guard(Guard), m_tiles() {}
    ~CMapTiles() { assert(m_guard == Guard); }

    void Check()
    {
#if defined(DEBUG)
        if (m_guard != Guard)
            DebugBreak();
#endif
    }

    void Resize(size_t x, size_t y)
    {
        Check();
        auto init = std::vector<std::vector<Tile_Base*> >(y/32);
        m_tiles.resize(m_x / 32, init);
        Check();
    }

    const maptiles_t& tiles() const { Check(); return m_tiles; }
    maptiles_t& tiles() { Check(); return m_tiles; }
};
typedef std::vector mapptiles\t;
类粒子
{
无符号整数m_-guard;
地图瓷砖;
枚举{Guard=0xdeadbeef};
公众:
cmoptiles():m_-guard(guard),m_-tiles(){}
~CMapTiles(){assert(m_guard==guard);}
无效检查()
{
#如果已定义(调试)
如果(m_guard!=guard)
DebugBreak();
#恩迪夫
}
空心调整大小(大小x,大小y)
{
检查();
自动初始化=标准::向量(y/32);
m_tiles.resize(m_x/32,init);
检查();
}
常量maptiles_t&tiles()常量{Check();返回m_tiles;}
maptiles_t&tiles(){Check();return m_tiles;}
};
而不是使用
std::vector map_tiles
have
cmoptiles map_tiles
,然后当您想要获取向量时,
map_tiles.tiles()


希望这能有所帮助。

文章顶部的“以下代码”既不可编译也没有意义。请纠正它。我是这样做的:“不管它是否有效,因为这个问题与它的功能无关,它只是一个例子。”但我还是纠正了它。我觉得你的阅读超出了有效的记忆范围。这是未定义的,因此在结构中移动元素将改变其行为。不幸的是,我非常怀疑您是否真的向我们展示了导致问题的代码……这里的诀窍是隔离(并简化)导致问题的代码,然后进行调试。引发异常时调用堆栈是什么样子的?当在内存中移动一个成员修复了这样一个错误时,它表明您已损坏,可能的原因是对象切片或使用memset/memcpy。好吧,我想这是可行的,尽管这是一个次要步骤,但我想这是我能提供的最佳解决方案。谢谢。如果这不是你需要的帮助,你不会接受它,这会减少它的曝光率:)我意识到这是一个远大的目标,但以这种方式呈现比在评论中更容易。当我得到确切的答案时,我会发布一个更好的解决方案;现在我只能靠自己了。不过,当我尝试您的解决方案并使用Check函数时,enum和m_guard之间的比较失败。啊-现在您可以在调试器中运行它,在Cmatiles构造函数中设置一个断点,然后在m_guard成员上设置一个数据监视,以便调试器将告诉您从何处覆盖它。(调试>断点)很抱歉,失败意味着m_guard仍然等于枚举。
typedef std::vector<std::vector<std::vector<Tile_Base*> > > maptiles_t;

class CMapTiles
{
    unsigned int m_guard;
    maptiles_t m_tiles;
    enum { Guard = 0xdeadbeef };
public:
    CMapTiles() : m_guard(Guard), m_tiles() {}
    ~CMapTiles() { assert(m_guard == Guard); }

    void Check()
    {
#if defined(DEBUG)
        if (m_guard != Guard)
            DebugBreak();
#endif
    }

    void Resize(size_t x, size_t y)
    {
        Check();
        auto init = std::vector<std::vector<Tile_Base*> >(y/32);
        m_tiles.resize(m_x / 32, init);
        Check();
    }

    const maptiles_t& tiles() const { Check(); return m_tiles; }
    maptiles_t& tiles() { Check(); return m_tiles; }
};