2012-07-25 125 views
2

这是我的代码,以找到在地图中的值:错误C2678:二进制“<”:没有操作员发现它接受一个左边的操作数...(或者没有可接受的转化率)

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>   ] 
+0

哪里是操作者<类型plVariablesConjunction? – ForEveR 2012-07-25 10:46:04

+0

您发布的代码不会在地图中插入任何内容,我敢打赌,这实际上与错误无关。 – 2012-07-25 10:47:02

+0

对不起,发布编辑 – DataMiner 2012-07-25 10:57:45

回答

7

std::map(通常)实现为二叉搜索树,最常见红黑树。它需要线性顺序来为关键值定义,以在树中找到正确的位置。这就是为什么std::map试图在插入的键值上调用operator<。您的班级不提供operator<。请为您的课程定义operator<或为模板提供比较功能:std::map<plVariablesConjunction, std::vector<plFloat>, my_comparison_function>

+0

是的,考虑到我的类变量连接,有没有其他的选择? – DataMiner 2012-07-25 11:00:20

+0

@ user986789:“其他选择”是什么意思?实现比较函数(作为函数,函子或操作符<'),或使用其他容器类型或使用另一种类型的'std :: map'。我不知道你的应用程序,现在也不在乎学习ProBT,所以我不知道哪种解决方案更好。 – 2012-07-25 11:30:48

+0

@ user986789:从什么ProBT(R)API ;-)文档的一瞥我见过,'plVariablesConjunction'是几个子类的基类。这意味着,除非1)你控制自己的执行,2)你真** **知道你在做什么,你不应该把它通过* *值到容器中。 'std :: map 2012-07-25 11:40:26

3

地图<>不使用operator==检查插入的值。它需要通过operator<来比较关键值。

+0

谢谢,plVariableConjunction数据类型不包含比较运算符< or >。它只是包含“==”,“=”,“=!”或“ - ” – DataMiner 2012-07-25 10:53:38

+0

@ user986789:要么你定义自己的运营商<该类型或你写仿函数这样做比较(两者都需要你有办法告诉两个连词中的哪一个“较小”)。如果你不能在它们之间得到订单,也许你需要一个不同的容器。 – 2012-07-25 11:00:29

3

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

std::map <key_type, data_type, [comparison_function]>

要么你需要提供一个比较函数或重载<运营商在重点班。

注意,比较功能是在括号,表明它是可选的,只要你为key_type具有小于操作,<,定义

相关问题