我一直在使用一组模板化函数contains
代码如下:
template<class V>
bool contains(const std::unordered_set<V>& c, const V& e)
{
return c.find(e) != c.cend();
}
我也有类似版本的std :: set,std :: map和std :: unordered_map
但是,我最近在使用时遇到了链接错误:
CMakeFiles/dietAgent.dir/agent/workflow/autoscale/AutoscaleCoreScheduler
.cc.o : Dans la fonction « bool contains<WfNode*, WfNode*>
(std::unordered_set<WfNode*, std::hash<WfNode*>, std::equal_to<WfNode*>,
std::allocator<WfNode*> > const&, WfNode* const&) » :
/home/amxx/Work/Thesis/Code/diet/src/utils/stdext.hh:51 : référence
indéfinie vers « std::integral_constant<bool, true> operator!=
<std::__detail::_Node_const_iterator<WfNode*, true, false>,
std::__detail::_Node_const_iterator<WfNode*, true, false> >
(std::__detail::_Node_const_iterator<WfNode*, true, false> const&,
std::__detail::_Node_const_iterator<WfNode*, true, false> const&) »
我从错误中理解的是迭代器没有operator!=
... WTF?
更新
显然,模板化的包装不应该受到责备。我尝试使用
if (mySet.find(value) != mySet.end()) { ... }
直接(而不是调用contains
),我得到了相同的undefined referennce to operator!=
答案 0 :(得分:-1)
在课堂上,您需要重载操作员!= 。
或者您可以使用运算符 == ,如下例所示:
template<class V>
bool contains(const std::unordered_set<V>& c, const V& e)
{
return !(c.find(e) == c.cend());
}