编译十六进制网格程序时错误代码C2768

Error code C2768 when compiling hex grid program

首先我想说我在编码方面的经验很少。只有一对 类 回到高中。我最近有了为我的 D&D 会话开​​发交互式游戏网格的想法。所以我安装了 Visual Studio 并找到了 Amit 的创建六角网格的指南:https://www.redblobgames.com/grids/hexagons/

我基本上是在复制代码并尝试边理解边理解它。我会先 post 整个程序,然后是我的编译错误所在的代码片段。

#include "stdafx.h"
#include <cmath>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <iterator>
#include <unordered_set>
#define M_PI 3.14159265358979323846264338327950288
using namespace std;


// Stores hex (cube) coordinates of a cell
struct Hex
{
    const int q, r, s;
    //Hex(int q_, int r_): q(q_), r(r_), s(-q_ - r_) {}
    Hex(int q_, int r_, int s_): q(q_), r(r_), s(s_) {
        if (q + r + s != 0) throw "q + r + s must be 0";
    }
};

// Stores pixel coordinates
struct Point
{
    const double x, y;
    Point(double x_, double y_): x(x_), y(y_) {}
};

// Stores fractional hex coordinates when converted from pixel coordinates
struct FractionalHex
{
    const double q, r, s;
    FractionalHex(double q_, double r_, double s_) : q(q_), r(r_), s(s_) {
        if (round(q + r + s) != 0) throw "q + r + s must equal 0";
    }
};

// Stores orientation of a cell
struct Orientation
{
    const double f0, f1, f2, f3;
    const double b0, b1, b2, b3;
    const double start_angle; // in multiples of 60deg
    Orientation(double f0_, double f1_, double f2_, double f3_, double b0_, double b1_, double b2_, double b3_, double start_angle_): f0(f0_), f1(f1_), f2(f2_), f3(f3_), b0(b0_), b1(b1_), b2(b2_), b3(b3_), start_angle(start_angle_) {}
};

// Stores layout information of a cell
struct Layout
{
    const Orientation orientation;
    const Point size;
    const Point origin;
    Layout(Orientation orientation_, Point size_, Point origin_): orientation(orientation_), size(size_), origin(origin_) {}
};


Hex hex_add(Hex a, Hex b)
{
    return Hex(a.q + b.q, a.r + b.r, a.s + b.s);
}

Hex hex_subtract(Hex a, Hex b)
{
    return Hex(a.q - b.q, a.r - b.r, a.s - b.s);
}

Hex hex_scale(Hex a, int k)
{
    return Hex(a.q * k, a.r * k, a.s * k);
}

int hex_length(Hex hex)
{
    return int((abs(hex.q) + abs(hex.r) + abs(hex.s)) / 2);
}

int hex_distance(Hex a, Hex b)
{
    return hex_length(hex_subtract(a, b));
}


// Rounds fractional hex coordinates with error correction
Hex hex_round(FractionalHex h)
{
    int qi = int(round(h.q));
    int ri = int(round(h.r));
    int si = int(round(h.s));
    double q_diff = abs(qi - h.q);
    double r_diff = abs(ri - h.r);
    double s_diff = abs(si - h.s);
    if (q_diff > r_diff && q_diff > s_diff)
    {
        qi = -ri - si;
    }
    else
        if (r_diff > s_diff)
        {
            ri = -qi - si;
        }
        else
        {
            si = -qi - ri;
        }
    return Hex(qi, ri, si);
}


// Linearly interpolates (lerp) between two cells
FractionalHex hex_lerp(FractionalHex a, FractionalHex b, double t)
{
    return FractionalHex(a.q * (1 - t) + b.q * t, a.r * (1 - t) + b.r * t, a.s * (1 - t) + b.s * t);
}

// Returns all cells in a straight line from a to b
vector<Hex> hex_linedraw(Hex a, Hex b)
{
    int N = hex_distance(a, b);
    FractionalHex a_nudge = FractionalHex(a.q + 0.000001, a.r + 0.000001, a.s - 0.000002);
    FractionalHex b_nudge = FractionalHex(b.q + 0.000001, a.r + 0.000001, a.s - 0.000002);
    vector<Hex> results = {};
    double step = 1.0 / max(N, 1);
    for (int i = 0; i <= N; i++)
    {
        results.push_back(hex_round(hex_lerp(a_nudge, b_nudge, step * i)));
    }
    return results;
}


// Returns desired angular direction based on 6 neighboring cells
const vector<Hex> hex_directions =
{
    Hex(1, 0, -1), Hex(1, -1, 0), Hex(0, -1, 1), Hex(-1, 0, 1), Hex(-1, 1, 0), Hex(0, 1, -1)
};
Hex hex_direction(int direction)
{
    return hex_directions[direction];
}

// Returns neighboring cell in a given direction
Hex hex_neighbor(Hex hex, int direction)
{
    return hex_add(hex, hex_direction(direction));
}


// Sets cell orientation to either flat top or pointy top
const Orientation layout_pointy = Orientation(sqrt(3.0), sqrt(3.0) / 2.0, 0.0, 3.0 / 2.0, sqrt(3.0) / 3.0, -1.0 / 3.0, 0.0, 2.0 / 3.0, 0.5);
const Orientation layout_flat = Orientation(3.0 / 2.0, 0.0, sqrt(3.0) / 2.0, sqrt(3.0), 2.0 / 3.0, 0.0, -1.0 / 3.0, sqrt(3.0) / 3.0, 0.0);

// Converts cell coordinates to pixel coordinates
Point hex_to_pixel(Layout layout, Hex h)
{
    Orientation M = layout.orientation;
    Point size = layout.size;
    Point origin = layout.origin;
    double x = (M.f0 * h.q + M.f1 * h.r) * size.x;
    double y = (M.f2 * h.q + M.f3 * h.r) * size.y;
    return Point(x + origin.x, y + origin.y);
}

// Converts pixel coordinates to cell coordinates, use with hex_round function
FractionalHex pixel_to_hex(Layout layout, Point p)
{
    Orientation M = layout.orientation;
    Point size = layout.size;
    Point origin = layout.origin;
    Point pt = Point((p.x - origin.x) / size.x, (p.y - origin.y) / size.y);
    double q = M.b0 * pt.x + M.b1 * pt.y;
    double r = M.b2 * pt.x + M.b3 * pt.y;
    return FractionalHex(q, r, -q - r);
}


// Returns pixel coordinates of a cell corner
Point hex_corner_offset(Layout layout, int corner)
{
    Orientation M = layout.orientation;
    Point size = layout.size;
    double angle = 2.0 * M_PI * (M.start_angle - corner) / 6;
    return Point(size.x * cos(angle), size.y * sin(angle));
}

// Returns vector of 6 corner coordinates of a cell
vector<Point> polygon_corners(Layout layout, Hex h)
{
    vector<Point> corners = {};
    Point center = hex_to_pixel(layout, h);
    for (int i = 0; i < 6; i++)
    {
        Point offset = hex_corner_offset(layout, i);
        corners.push_back(Point(center.x + offset.x, center.y + offset.y));
    }
    return corners;
}


// Hash function for Hex, used for map storage
    template <> struct hash<Hex>
    {
        size_t operator()(const Hex& h) const
        {
            hash<int> int_hash;
            size_t hq = int_hash(h.q);
            size_t hr = int_hash(h.r);
            return hq ^ (hr + 0x9e3779b9 + (hq << 6) + (hq >> 2));
        }
    };


int main()
{
    int map_height = 5;
    int map_width = 5;
    unordered_set<Hex> map;
    for (int q = 0; q < map_height; q++)
    {
        int q_offset = q >> 1;
        for (int s = -q_offset; s < map_width - q_offset; s++)
        {
            map.insert(Hex(q, -s - q, s));
        }
    }
}

我有一个编译错误 C2678 二进制“==”:没有找到接受类型 'const Hex' 的左手操作数的运算符(或者没有可接受的转换)

指向这条线:return hex_directions[方向];

const vector<Hex> hex_directions =
{
    Hex(1, 0, -1), Hex(1, -1, 0), Hex(0, -1, 1), Hex(-1, 0, 1), Hex(-1, 1, 0), Hex(0, 1, -1)
};
Hex hex_direction(int direction)
{
    return hex_directions[direction];
}

我试着查找错误代码,但我不太明白如何修复它。任何见解将不胜感激。

嗯,这个错误对我来说很有意义,尽管我实际上认为您可能误读了错误消息的一部分,因为您发布的代码段肯定与 operator==.

无关

您收到的错误是因为您使用的是 std::unordered_set<Hex>。虽然看起来您已经正确创建了一个专门的 std::hash<Hex> 结构,但为了启用散列,std::unordered_set 还需要一个 operator == 重载,以检查散列冲突。

如果您不熟悉,operator== 允许您为 Hex 类型定义 "equality"。它允许您编写此代码:

// Check if a hex coordinate is the origin, that is, (0,0,0)
bool is_origin(const Hex& h)
{
    return h == Hex{0,0,0};
}

此运算符的一个简单实现如下所示:

bool operator==(const Hex& lhs, const Hex& rhs)
{
    return lhs.q == rhs.q && lhs.r == rhs.r;
}

但是,我承认我不熟悉您在这里使用的立方坐标系。我强烈建议您查看 this excellent guide 六角坐标系,以确保您能很好地掌握该系统。