c++ - 我的代码不适用于 std::map 和 sf::Vector2i

标签 c++ dictionary stl sfml stdmap

我正在尝试为 SFML 制作寻路系统,但由于编译错误而卡住了。当我尝试将元素添加到我的 std::map 时会发生此错误。这是标题代码:

#include <SFML/Graphics.hpp>
#include <list>
#include <map>

class Node {
    public: 
        float cout_g, cout_h, cout_f;
        sf::Vector2i parent;
};

class Pathfinding
{
    public: 
        Pathfinding(sf::Vector2i);
        std::list<sf::Vector2i> searchPath(sf::Vector2i endpoint,sf::Vector2i startpoint);

    private: 
        std::map<sf::Vector2i,Node> closedList;
        std::map<sf::Vector2i,Node> openList;
};

这里是源代码:

#include "Pathfinding.h"

Pathfinding::Pathfinding(sf::Vector2i coords)
{
}

std::list<sf::Vector2i> Pathfinding::searchPath(sf::Vector2i endpoint, sf::Vector2i startpoint)
{
    Node startNode;
    startNode.parent.x = 0;
    startNode.parent.y = 0;
    openList[startpoint] = startNode;
    std::list<sf::Vector2i> list;
    return list;
}

这是游戏循环:

#include "Pathfinding.h"

int main()
{
    sf::RenderWindow window(sf::VideoMode(800,600),"A* Test");
    Pathfinding pathfinder(sf::Vector2i(800,600));
    while(window.isOpen())
    {
        sf::Event event;
        while(window.pollEvent(event))
        {
            if(event.type == sf::Event::Closed) window.close();
        }
        std::list<sf::Vector2i> path = pathfinder.searchPath(sf::Vector2i(3,3),sf::Vector2i(45,55));
        window.clear(sf::Color::White);
        window.display();
    }
    return 0;
}

这段代码根本不起作用,为了调试我把它缩减到最低限度。
我真的不明白它给出的错误代码:http://pastebin.com/mBVALHML (我将其发布在 Pastebin 上,因为它真的很长)。我在这个错误中唯一理解的是问题来自这一行:

openList[startpoint] = startNode;

我还尝试使用 SFML 2.1 和 2.2 进行编译,但没有成功。那么您知道我为什么会收到此错误,以及如何解决它吗? 非常感谢:)

最佳答案

sf::Vector2<T>没有operator<但为了将它用作 std::map 中的键它需要这样的运营商。 你有两种选择,无需修改 Vector2.hpp :一种复杂而简单但不太想要的方式。

简单

只需制作 map来自固定大小的 s,例如

/*some function-head-thing*/(sf::Vector2u size)
{
    for(unsigned int y = 0U; y < size.y; ++y)
        for(unsigned int x = 0U; x < size.x; ++x)
            map[x + y * size.x] = /*some init value*/
}

为了访问 map 中的元素,您始终需要知道大小,但它很简单:map[x + y * size.x] .

复杂

作为 operator==sf::Vector2<T> 定义你只需要添加一个 std::hash指定用于 sf::Vector2<T>然后你可以用 std::unordered_map 替换 map . 也许是这样的:

namespace std
{
    template <class T>
    struct hash<sf::Vector2<T>>
    {
        std::size_t operator()(const sf::Vector2<T>& v) const
        {
            using std::hash;

            // Compute individual hash values for first
            // and second. Combine them using the Boost-func

            std::size_t tmp0 = hash<T>()(v.x);
            std::size_t tmp1 = hash<T>()(v.y);

            tmp0 ^= tmp1 + 0x9e3779b9 + (tmp0 << 6) + (tmp0 >> 2);
         }
    };
}

但是如果你想使用 sf::Vector2f 就要小心了!最好加个static_assert限制 T 的使用, 它不应该是 float operator==可能不会给出预期的结果,无论是否进行模糊比较。

否则

添加一些 operator<Vector2.hppVector2.inl ,但是您需要它。

关于c++ - 我的代码不适用于 std::map 和 sf::Vector2i,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27553850/

相关文章:

java - 如何编写一个Map的set方法并将另一个Map作为值?

python - 字典中的一把 key 。需要该键的第四个值吗?

c++ - 无效的模板相关成员函数模板推导 - 认为我正在尝试使用 std::set

c++ - 与列表初始化语法的自动分配混淆

c++ - 尝试包含 '#include <boost/regex.hpp>' 时,我得到 : 1>LINK : fatal error LNK1104: cannot open file 'libboost_regex-vc100-mt-gd-1_39.lib'

c++ - 防止其他线程读取资源

c++ - 哪一位是第一位,当你位移时,它实际上是朝那个方向移动的吗?

python - 从文本文件创建深度嵌套的字典

c++ - 第 5 行 : Char 54: error: no matching function for call to 'min(int, std::__cxx11::basic_string<char>::size_type)'

c++ - 为什么 remove_if( ..., lambda ) 表达式需要赋值运算符?