6

Here is my code to to find values in a map:

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" is a ProBT library datatype. it contains operator "==", if two variables found same then it returns true otherwise false.

Here is error:

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>          ]
DataMiner
  • 325
  • 1
  • 3
  • 16

3 Answers3

13

std::map is (usually) implemented as binary search tree, most often red-black tree. It needs linear order to be defined for key values to find correct position within a tree. That's why std::map tries to call operator< on inserted key values.

Your class doesn't provide operator<. Either define operator< for your class or provide comparison function for template: std::map<plVariablesConjunction, std::vector<plFloat>, my_comparison_function>.

Tomek Szpakowicz
  • 14,063
  • 3
  • 33
  • 55
  • Yes, considering my class of plVariableConjunction, is there other alternative? – DataMiner Jul 25 '12 at 11:00
  • @user986789: What do you mean by 'other alternatives'? Implement comparison function (as function, functor or `operator<`) or use another container type or use another key type for `std::map`. I don't know your application and don't care about learning ProBT right now, so I can't tell which solution is better. – Tomek Szpakowicz Jul 25 '12 at 11:30
  • @user986789: From what a glimpse of ProBT(R)API ;-) docs I've seen, `plVariablesConjunction` is a base class for several subclasses. That means that unless 1) you control their implementation and 2) you **really** know what you're doing, you shouldn't put it *by value* into containers. `std::map – Tomek Szpakowicz Jul 25 '12 at 11:40
5

To use the map class, require two, and possibly three, types for the template:

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

Either you need to provide a comparison function or overload the < operator in the key class.

Notice that the comparison function is in brackets, indicating that it is optional so long as your key_type has the less-than operator, <, defined

ThirdOne
  • 1,227
  • 9
  • 13
4

map<> doesn't use operator== to check the inserted values. It needs a comparision via operator< for the key values.

C. Stoll
  • 376
  • 1
  • 6
  • Thanks, plVariableConjunction data type does not contains comparison operator < or >. It just contains "==", "=", "!=" or "-" – DataMiner Jul 25 '12 at 10:53
  • @user986789: either you define your own operator< for the type or you write a functor doing this comparision (both require you have a way to tell which of two Conjunctions is "smaller"). If you can't get an order between them, maybe you need a different container. – C. Stoll Jul 25 '12 at 11:00