2

I'm a second year college student in Video Game Developement. My teacher doesn't even know how to solve this so I'm hoping somebody here could help! Thank you in advance!

I'm developing a video game and I need pathfinding for the enemies. It's required for me to use A* pathfinding.

I decided to use unordered maps because I figured it's more efficient. I did all the code and it seeemed to be right but then I got an error from the unordered_map library:

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__

This is 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);
}

This is C++ in Visual Studio. If anyone can help I would appreciate it a lot! Thank you again!

ocrdu
  • 2,172
  • 6
  • 15
  • 22
  • Does this answer your question? [Compilation error related to map and unordered\_map: "attempting to reference a deleted function"](https://stackoverflow.com/questions/51220257/compilation-error-related-to-map-and-unordered-map-attempting-to-reference-a-d) – Lucas Charbonnier Nov 30 '20 at 14:44
  • 1
    This doesn't address the question, but names that contain two consecutive underscores (`__PATHFINDING_H__`) and names that begin with an underscore followed by a capital letter are reserved for use by the implementation. Don't use them in your code. – Pete Becker Nov 30 '20 at 14:45
  • 1
    The potential duplicate is a bit more focused on hashing of `vector`, but anyway: you need to write a hash function for `iPoint`, which can reasonably be e.g. `namespace std { template <> struct hash { auto operator()(const iPoint& point) const { return point.x + (point.y << 6) + (point.y >> 2); } }; }`. You can replace the `#ifndef/#define/#endif` with `#pragma once` and avoid the issue Pete mentions above. – Tony Delroy Nov 30 '20 at 15:26
  • 1
    Change your teacher at the earliest convenience. The fact that `std::hash` needs to be "implemented" for custom structures shouldn't be news to anyone using C++11, let alone "very weird". – Daniel Kamil Kozar Dec 01 '20 at 00:02
  • Thank you all! I added the hash function for the iPoint and it works! – Maria Avenida Dec 01 '20 at 13:55

1 Answers1

2

See the answer from John Zwinck on this other question.

Basically, the key (an iPoint) of your unordered_map needs to be "hashable". So you need to implement a hash function for it but I don't know exactly how you do that.

Lucas Charbonnier
  • 441
  • 1
  • 4
  • 11