我是视频游戏开发专业的二年级学生。我的老师甚至不知道如何解决这个问题,所以我希望有人能帮忙!预先感谢您!
我正在开发一款视频游戏,我需要为敌人寻路。我需要使用 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 }
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/