【问题标题】:error C2678: binary '<' : no operator found which takes a left-hand operand... (or there is no acceptable conversion) [duplicate]错误 C2678:二进制“<”:未找到采用左操作数的运算符...(或没有可接受的转换)[重复]
【发布时间】:2012-07-23 18:42:53
【问题描述】:

这是我在地图中查找值的代码:

bool myclass::getFreqFromCache( plVariablesConjunction& varABC, vector<plFloat>& freq )
{
std::map<plVariablesConjunction, std::vector<plFloat>>::iterator freqItr;
    freqItr = freqCache.find(varABC);

    if (freqItr != freqCache.end())
        {
        freq = freqItr->second;
        return true;
        }
 }

“PlVariablesConjunction”是 ProBT 库数据类型。它包含运算符“==”,如果发现两个变量相同,则返回 true,否则返回 false。

这里是错误:

C:\Program Files\Microsoft Visual Studio 10.0\VC\include\xfunctional(125): error C2678: binary '<' : no operator found which takes a left-hand operand of type 'const plVariablesConjunction' (or there is no acceptable conversion)
1>          E:\ProBT22\probt-spl-2.2.0-expires-20121130-vc10-dynamic-release\include\plSymbol.h(71): could be 'bool operator <(const plSymbol &,const plSymbol &)' [found using argument-dependent lookup]
1>          while trying to match the argument list '(const plVariablesConjunction, const plVariablesConjunction)'
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\xfunctional(124) : while compiling class template member function 'bool std::less<_Ty>::operator ()(const _Ty &,const _Ty &) const'
1>          with
1>          [
1>              _Ty=plVariablesConjunction
1>          ]
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\map(71) : see reference to class template instantiation 'std::less<_Ty>' being compiled
1>          with
1>          [
1>              _Ty=plVariablesConjunction
1>          ]
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\xtree(451) : see reference to class template instantiation 'std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,_Mfl>' being compiled
1>          with
1>          [
1>              _Kty=plVariablesConjunction,
1>              _Ty=std::vector<plProbValue>,
1>              _Pr=std::less<plVariablesConjunction>,
1>              _Alloc=std::allocator<std::pair<const plVariablesConjunction,std::vector<plProbValue>>>,
1>              _Mfl=false
1>          ]
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\xtree(520) : see reference to class template instantiation 'std::_Tree_nod<_Traits>' being compiled
1>          with
1>          [
1>              _Traits=std::_Tmap_traits<plVariablesConjunction,std::vector<plProbValue>,std::less<plVariablesConjunction>,std::allocator<std::pair<const plVariablesConjunction,std::vector<plProbValue>>>,false>
1>          ]
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\xtree(659) : see reference to class template instantiation 'std::_Tree_val<_Traits>' being compiled
1>          with
1>          [
1>              _Traits=std::_Tmap_traits<plVariablesConjunction,std::vector<plProbValue>,std::less<plVariablesConjunction>,std::allocator<std::pair<const plVariablesConjunction,std::vector<plProbValue>>>,false>
1>          ]
1>          C:\Program Files\Microsoft Visual Studio 10.0\VC\include\map(81) : see reference to class template instantiation 'std::_Tree<_Traits>' being compiled
1>          with
1>          [
1>              _Traits=std::_Tmap_traits<plVariablesConjunction,std::vector<plProbValue>,std::less<plVariablesConjunction>,std::allocator<std::pair<const plVariablesConjunction,std::vector<plProbValue>>>,false>
1>          ]
1>          e:\probt22\work\yasin\testmmhcfinalversion\testmmhc_mi_probt_sw\mmhc\slidingWindow.h(55) : see reference to class template instantiation 'std::map<_Kty,_Ty>' being compiled
1>          with
1>          [
1>              _Kty=plVariablesConjunction,
1>              _Ty=std::vector<plProbValue>
1>          ]

【问题讨论】:

  • 对于类型 plVariablesConjunction 的运算符
  • 你发布的代码没有在地图中插入任何东西,我敢打赌它实际上与错误无关。

标签: c++ map


【解决方案1】:

map 不使用operator== 检查插入的值。它需要通过operator&lt; 对键值进行比较。

【讨论】:

  • 谢谢,plVariableConjunction 数据类型不包含比较运算符 。它只包含“==”、“=”、“!=”或“-”
  • @user986789:要么为类型定义自己的 operator
【解决方案2】:

要使用地图类,模板需要两种,也可能是三种类型:

std::map &lt;key_type, data_type, [comparison_function]&gt;

要么提供比较函数,要么重载key类中的

注意比较函数在括号中,表明它是可选的,只要你的key_type有小于运算符,

【讨论】:

    【解决方案3】:

    std::map(通常)被实现为二叉搜索树,最常见的是红黑树。它需要为键值定义线性顺序才能在树中找到正确的位置。这就是为什么std::map 尝试在插入的键值上调用operator&lt;

    您的班级不提供operator&lt;。为您的班级定义operator&lt; 或为模板提供比较功能:std::map&lt;plVariablesConjunction, std::vector&lt;plFloat&gt;, my_comparison_function&gt;

    【讨论】:

    • 是的,考虑到我的 plVariableConjunction 类,还有其他选择吗?
    • @user986789:你所说的“其他选择”是什么意思?实现比较函数(作为函数、函子或operator&lt;)或使用另一种容器类型或为std::map 使用另一种键类型。我不知道你的应用,也不关心现在学习 ProBT,所以我不知道哪个解决方案更好。
    • @user986789:从我所看到的 ProBT(R)API ;-) 文档的一瞥中,plVariablesConjunction 是几个子类的基类。这意味着除非 1) 你控制它们的实现,并且 2) 你真的知道你在做什么,你不应该把它按值放入容器中。 std::map&lt;plVariablesConjunction, ... 表示容器内的值持有的实例。这是等待发生的内存损坏或数据截断。 (进一步提示:未定义复制构造函数。)使用其他键类型。它应该是:1)简单的按值类型,2)可比较的,3)(实际上)不可变的。
    猜你喜欢
    • 2014-12-04
    • 2017-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-09
    • 2015-11-18
    相关资源
    最近更新 更多