C++ 显式默认的默认构造函数将被隐式删除,因为将结构用作键时使用了无序的_映射

C++ 显式默认的默认构造函数将被隐式删除,因为将结构用作键时使用了无序的_映射,c++,unordered-map,default-constructor,C++,Unordered Map,Default Constructor,我有以下图形类: class Graph { private: struct Edge { string vertex1{}, vertex2{}; int val{}; Edge() = default; ~Edge() = default; explicit Edge(string v1, string v2, int value) : vertex1(std::move(v1)), vertex2(s

我有以下图形类:

class Graph {
private:
    struct Edge {
        string vertex1{}, vertex2{};
        int val{};

        Edge() = default;
        ~Edge() = default;
        explicit Edge(string v1, string v2, int value) : vertex1(std::move(v1)), vertex2(std::move(v2)), val(value) {};
        bool operator==(const Edge&) const;
    };

    unordered_map<Edge, Edge*> edges;

public:
    Graph() = default;
    ~Graph();
}
类图{
私人:
结构边{
字符串vertex1{},vertex2{};
int val{};
Edge()=默认值;
~Edge()=默认值;
显式边(字符串v1,字符串v2,int值):顶点1(std::move(v1)),顶点2(std::move(v2)),val(value){};
布尔运算符==(常数边和)常数;
};
无序的地图边缘;
公众:
Graph()=默认值;
~Graph();
}
当我想用默认构造函数构造一个图时,它会说,
显式默认的“Graph”默认构造函数被隐式删除,因为字段“edges”有一个已删除的默认构造函数
。我应该如何更改代码才能使用Graph的默认构造函数?

无序映射中的键需要可散列。通过添加
std::hash
类模板的专门化,或者在创建
unordered_map
时提供散列函数

您没有显示您已经进行了
std::hash
专门化,并且没有使用函子创建
unordered_map
来进行哈希,这就是默认构造失败的原因

要添加
std::hash
专门化,可以这样做(如果将
Edge
公开):

namespace std {

template<>
struct hash<Graph::Edge> {
    size_t operator()(const Graph::Edge& e) const {
        size_t hash_result;
        // calculate the hash result
        return hash_result;
    }
};

} // namespace std
class Graph {
private:
    struct Edge {
        // ...
    };

    // the hashing functor:
    struct edge_hasher {
        size_t operator()(const Edge& e) const {
            // an example implementation using boost::hash_combine
            // from <boost/container_hash/hash.hpp>:
            std::hash<std::string> h;
            size_t hash_result = 0;
            boost::hash_combine(hash_result, h(e.vertex1));
            boost::hash_combine(hash_result, h(e.vertex2));
            boost::hash_combine(hash_result, std::hash<int>{}(e.val));
            return hash_result;
        }
    };

    std::unordered_map<Edge, Edge*, edge_hasher> edges; // <- hasher included

public:
    Graph() = default;
    ~Graph();
}