是否可以将指向unordered_set的指针<MyClass>作为 myClass 的一部分?
Is it possible to have pointer to unordered_set<MyClass> as a part of myClass?
我的代码看起来像这样:
class Node : public bitset<BITS_COUNT> {
public:
Node(string s) : bitset<BITS_COUNT>(s) {}
void setGroup(unordered_set<Node>* newSet) { currentGroup = newSet; }
unordered_set<Node>* getCurrentSet() { return currentGroup; }
private:
unordered_set<Node>* currentGroup = nullptr;
};
但是编译器不允许我这样做,因为没有为类节点定义哈希函数。我希望它使用基类中的哈希函数,所以我这样做了:
namespace std
{
template<>
struct hash<Node>
{
size_t operator()(const Node& k) const
{
return k.hash();
}
};
}
但它仍然不起作用。如果我把它放在 Node delcaration 之前,k.hash() 是未定义的(我不能转发声明 Node:public bitset<>因为它是不可能的)。如果我把它放在类声明之后,我会收到错误,即类节点没有哈希函数。
如何解决这个问题?
谢谢弗兰克,你的评论对我来说实际上是一个解决方案。如果有人需要代码,它看起来像这样:
namespace std
{
template<>
struct hash<Node>
{
size_t operator()(const Node& k) const;
};
}
class Node : public std::bitset<BITS_COUNT> {
public:
Node(std::string s) : bitset<BITS_COUNT>(s) {}
void setGroup(std::unordered_set<Node>* newSet) { currentGroup = newSet; }
std::unordered_set<Node>* getCurrentSet() { return currentGroup; }
private:
std::unordered_set<Node>* currentGroup = nullptr;
};
namespace std
{
size_t hash<Node>::operator()(const Node& k) const
{
return k.hash();
}
}
相关文章:
- EASTL矢量<向量<int>>连续的
- 如何创建一个类,以便向量工作 std::vector<MyClass<int>> v{ 1,2,3 };
- 反转C++ foo(MyClass &) vs foo(const MyClass &)
- 从 json 字符串将 QVariant 转换为 QList<MyClass>
- #include "MyClass.h" 和 #include ".myclass.h" 有什么区别
- MyClass obj = MyClass();'MyClass()'在这里指的是临时对象吗?
- 'return':无法从'const class myClass'转换为'class myClass &'
- 如何初始化 const std::vector<MyClass>
- "delete p"和"p->~MyClass()"之间的区别
- C - 创建矢量&lt; vector&lt; double&gt;&gt;矩阵具有分配而不是inizializ
- 'class MyClass' 在尝试定义静态结构实例时不会命名类型
- 注意:候选:MyClass:MyClass(const MyClass&) 错误
- C 字符串比较“祝您好运”&gt;“再见”
- std::set<myClass*>排序
- 复制构造函数语法错误:myclass 没有成员 mymember
- 为什么将此对向量&lt; map&lt; int,int&gt;&gt;中的地图进行更新.失败
- 如何通过扩展以下类型特征来删除 decltype(&MyClass::funct) 部分?
- Qt C++ - 创建 QList<Myclass> - 不是参数 'T 的有效模板类型参数
- C :对矢量进行排序&lt; struct&gt;(结构有2个整数)基于结构的整数之一
- C 操作员&gt;&gt;与突变器过载