我想创建一个以std::unordered_map
为键的Node
。我已经使运算符==
和<
重载了。我还定义了一个哈希函数。但是,这给了我错误:
C2280: std::hash<_Kty>::hash(const std::hash<_Kty> &): attempting to reference a deleted function
这是我的Node
类的代码:
#include "Node.h"
Node::Node() {
word = Word();
}
Node::Node(Word input_word) : word(input_word) {}
Node::Node(const Node& other) {
word = other.word;
connection = other.connection;
}
Node::~Node() {
}
bool Node::operator<(const Node& other) {
return (word < other.get_word());
}
bool Node::operator==(const Node& other) {
if (word == other.word) {
if (connection.size() == other.connection.size()) {
for (unsigned int i = 0; i < connection.size(); i++) {
if (connection[i] != other.connection[i]) {
return false;
}
}
return true;
} else {
return false;
}
} else {
return false;
}
}
Word Node::get_word() const {
return word;
}
int Node::add_connection(Edge* input_node) {
connection.push_back(input_node);
return SUCCESS;
}
vector<Edge*> Node::retrieve_connection() const{
return connection;
}
namespace std {
template <> struct hash<Node> {
size_t operator()(const Node& other) const {
return hash<string>()(other.get_word().get_string()) ^
hash<int>()(other.retrieve_connection().size());
}
};
}
我对接下来要添加的内容感到非常困惑。我尝试了我在网站上可以找到的大多数建议,但都没有用。
谢谢您的关注!