C++自定义类的无序集-插入时segfault

hgb9j2n6  于 2022-12-05  发布在  其他
关注(0)|答案(1)|浏览(121)

我正在学习一个关于为游戏创建六边形Map的教程。源代码中有它的结构,但我希望它是一个类,但到目前为止我还不能使它工作。它编译得很好,但当我试图在其中插入一个新值时,它发生了segfaults。我可能是做错了哈希函数,或其他什么,但我已经没有办法修复它了。谢谢!
main.c

#include <unordered_set>
#include "hexagonField.hpp"

int main(int argc, char **argv)
{
    std::unordered_set <HexagonField> map;
    map.insert(HexagonField(0, 0, 0));
            
    return 0;
}

hexagonField.hpp

#ifndef HEXAGON_H
#define HEXAGON_H
#include <assert.h>
#include <vector>

class HexagonField
{
public:
    const int q, r, s;
    HexagonField(int q, int r, int s);
    ~HexagonField();
    HexagonField hexagonAdd(HexagonField a, HexagonField b);
    HexagonField hexagonSubtract(HexagonField a, HexagonField b);
    HexagonField hexagonMultiply(HexagonField a, int k);
    int hexagonLength(HexagonField hex);
    int hexagonDistance(HexagonField a, HexagonField b);
    HexagonField hexagonDirection(int direction /* 0 to 5 */);
    HexagonField hexagonNeighbor(HexagonField hex, int direction);

    const std::vector<HexagonField> hexagonDirections = {
    HexagonField(1, 0, -1), HexagonField(1, -1, 0), HexagonField(0, -1, 1),
    HexagonField(-1, 0, 1), HexagonField(-1, 1, 0), HexagonField(0, 1, -1)
    };

    bool operator == (const HexagonField comparedHex) const
    {
        return this->q == comparedHex.q && this->r == comparedHex.r && this->s == comparedHex.s;
    }
    bool operator != (const HexagonField comparedHex) const
    {
        return !(*this == comparedHex);
    };
};

namespace std
{
  template<>
    struct hash<HexagonField>
    {
      size_t operator()(const HexagonField & obj) const
      {
        return hash<int>()(obj.q);
      }
    };
}
#endif

hexagonField.cpp

#include "hexagonField.hpp"

HexagonField::HexagonField(int q, int r, int s): q(q), r(r), s(s)
{
    assert (q + r + s == 0);
}

HexagonField HexagonField::hexagonAdd(HexagonField a, HexagonField b)
{
    return HexagonField(a.q + b.q, a.r + b.r, a.s + b.s);
}

HexagonField HexagonField::hexagonSubtract(HexagonField a, HexagonField b)
{
    return HexagonField(a.q - b.q, a.r - b.r, a.s - b.s);
}

HexagonField HexagonField::hexagonMultiply(HexagonField a, int k)
{
    return HexagonField(a.q * k, a.r * k, a.s * k);
}

int HexagonField::hexagonLength(HexagonField hex) {
    return int((abs(hex.q) + abs(hex.r) + abs(hex.s)) / 2);
}

int HexagonField::hexagonDistance(HexagonField a, HexagonField b) {
    return hexagonLength( hexagonSubtract(a, b));
}

HexagonField HexagonField::hexagonDirection(int direction /* 0 to 5 */) {
    assert (0 <= direction && direction < 6);
    return hexagonDirections[direction];
}

HexagonField HexagonField::hexagonNeighbor(HexagonField hex, int direction) {
    return hexagonAdd(hex, hexagonDirection(direction));
}
2sbarzqh

2sbarzqh1#

如果你在调试器下运行程序,你会发现它在试图构造HexagonField对象时溢出了堆栈。这是因为每个对象都有一个由6个HexagonField对象组成的向量,而这又需要另一个向量,依此类推。
作为一种快速解决方法,您可以将hexagonDirectionsHexagonField类中取出,并将其移动到HexagonField.cpp顶部的静态文件作用域变量中:

static const std::vector<HexagonField> hexagonDirections = {
HexagonField(1, 0, -1), HexagonField(1, -1, 0), HexagonField(0, -1, 1),
HexagonField(-1, 0, 1), HexagonField(-1, 1, 0), HexagonField(0, 1, -1)
};

或者,您可以将hexagonDirections保留为静态类别成员,并将定义移至您的.cpp档案:
第一次

相关问题