c++ - 无序映射有错误,提示该函数本身不存在于 C++ 库中

标签 c++ unordered-map

我是视频游戏开发专业的二年级学生。我的老师甚至不知道如何解决这个问题,所以我希望有人能帮忙!预先感谢您!

我正在开发一款视频游戏,我需要为敌人寻路。我需要使用 A* 寻路。

我决定使用无序映射,因为我认为它更有效。我完成了所有代码,它似乎是正确的,但后来我从 unordered_map 库收到错误:

Error  C2280   'std::hash::hash(const std::hash &)': attempting to reference a deleted function.
        with
        [
            _Kty=iPoint
        ]   Game    C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\unordered_map   117 

This error then takes me to the unordered_map file, specifically to this line:

unordered_map() : _Mybase(_Key_compare(), allocator_type()) { // construct empty map from defaults
    }

enter image description here

My teacher says it's very weird and can't fix it. The other thing is that I decided to use unordered_maps but it's not the way the teacher told us to do it. He has no problem with me doing it differently, but that also means that I have no other reference from other classmates because they used a different method (do everything with lists).

I will put here the pathfinding class I coded:

This is PathFinding.h:

#ifndef __PATHFINDING_H__
#define __PATHFINDING_H__

#include "Module.h"

#include "Point.h"
#include "DynArray.h"
#include "List.h"
#include <unordered_map>
#include <queue> 


class PathFinding : public Module
{
public:

    PathFinding();
    ~PathFinding();

    int Heuristic(iPoint, iPoint);
    iPoint Path(iPoint, int);
    iPoint NextMove(std::unordered_map<iPoint, iPoint>, iPoint, iPoint);
};

#endif // __PATHFINDING_H__

这是 PathFinding.cpp:

#include "App.h"
#include "PathFinding.h"
#include "Player.h"
#include "List.h"
#include <unordered_map>
#include <queue> 

#include "Defs.h"
#include "Log.h"

PathFinding::PathFinding() : Module() { name.Create("pathfinding"); }

PathFinding::~PathFinding(){}

int PathFinding::Heuristic(iPoint start, iPoint end)
{
    return abs(start.x - end.x) + abs(start.y - end.y);
}

List<iPoint> Neighbours(iPoint current, int speed)
{
    List<iPoint> list = List<iPoint>();
    // Order is top, right, bottom, left
    list.Add(iPoint(current.x, current.y - speed));
    list.Add(iPoint(current.x + speed, current.y));
    list.Add(iPoint(current.x, current.y - speed));
    list.Add(iPoint(current.x - speed, current.y));

    return list;
}

iPoint PathFinding::NextMove(std::unordered_map<iPoint, iPoint> cameFrom, iPoint end, iPoint start) 
{
    iPoint current = end;

    while (cameFrom[current] != start)
    {
        current = cameFrom[current];
    }
    return current; 
}

iPoint PathFinding::Path(iPoint start, int speed) 
{   
    PriorityQueue<iPoint, double> frontier;
    std::unordered_map<iPoint, iPoint> came_from; 
    std::unordered_map<iPoint, int> cost_so_far;
    frontier.put(start, 0);
    iPoint end = app->player->playerPos;
    came_from[start] = start;
    cost_so_far[start] = 0;

    while (!frontier.empty()) 
    {
        iPoint current = frontier.get();

        if (current == end)
        {
            break;
        }
        List<iPoint> neighbours = Neighbours(current, speed);
        for (int i = 0; i < 4; i++) 
        {
            double new_cost = cost_so_far[current] + speed;
            iPoint next = neighbours.At(i)->data;
            if (cost_so_far.find(next) == cost_so_far.end() || new_cost < cost_so_far[next]) 
            {
                cost_so_far[next] = new_cost;
                double priority = new_cost + Heuristic(next, end);
                frontier.put(next, priority);
                came_from[next] = current;
            }
        }
    }
    return NextMove(came_from, end, start);
}

这是 Visual Studio 中的 C++。如果有人可以提供帮助,我将非常感激!再次感谢您!

最佳答案

请参阅 John Zwinck 对此的回答 other question .

基本上,unordered_map 的键(iPoint)需要是“可散列的”。所以你需要为它实现一个哈希函数,但我不知道你到底是如何做到的。

关于c++ - 无序映射有错误,提示该函数本身不存在于 C++ 库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65073680/

相关文章:

c++ - 使用 Xojo 进行跨平台开发有哪些妥协?

c++ - 派生类中的使用声明不会隐藏从基类派生的相同函数

c++ - 指向同一对象的不同基类型的指针和唯一指针

c++ - 如何从用户应用程序向 SYSTEM 应用程序发送自定义消息?

c++ - boost::unordered_map.emplace(Args&&... args) 如何工作?

c++ - 在循环中执行移位操作时 C++ 中的内存泄漏

c++ - std::unordered_map::insert 什么时候会失败?

使用命名空间创建 C++ 库

C++ 在无序对中存储值

c++ - 如何散列 unordered_map?