未定义对运算符的引用!= 使用 std::unordered_set 时

Undefined reference to operator!= when using std::unordered_set

本文关键字:std unordered set 使用 运算符 引用 未定义      更新时间:2023-10-16

我一直在使用一组模板化函数contains

代码如下:

template<class V>
bool contains(const std::unordered_set<V>& c, const V& e)
{
    return c.find(e) != c.cend();
}

我也有类似的标准::设置,标准::地图和标准::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!=......跆拳道?

更新:

显然,模板包装器不应该受到责备。我试过使用

if (mySet.find(value) != mySet.end()) { ... }

直接(而不是打电话给contains(,我得到了同样的undefined referennce to operator!=

在你的类中,你需要重载运算符!=

或者,您可以使用运算符 ==,如以下示例所示:

template<class V>
bool contains(const std::unordered_set<V>& c, const V& e)
{
    return !(c.find(e) == c.cend());
}