0
votes

I am creating a map data structure that uses set to hold pair values. I have made a custom pair class for the program. I got through most of the debugging and now i am getting these error in the built in xfunction class.

Error 6 error C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'const Pair' c:\program files (x86)\microsoft visual studio 10.0\vc\include\xfunctional 125

Error 7 error C2784: 'bool std::operator <(const _Elem *,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const _Elem *' from 'const Pair' c:\program files (x86)\microsoft visual studio 10.0\vc\include\xfunctional 125

Error 8 error C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'const Pair' c:\program files (x86)\microsoft visual studio 10.0\vc\include\xfunctional 125

Error 9 error C2784: 'bool std::operator <(const std::move_iterator<_RanIt> &,const std::move_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::move_iterator<_RanIt> &' from 'const Pair' c:\program files (x86)\microsoft visual studio 10.0\vc\include\xfunctional 125

Error 10 error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const Pair' c:\program files (x86)\microsoft visual studio 10.0\vc\include\xfunctional 125

Error 11 error C2784: 'bool std::operator <(const std::list<_Ty,_Ax> &,const std::list<_Ty,_Ax> &)' : could not deduce template argument for 'const std::list<_Ty,_Ax> &' from 'const Pair' c:\program files (x86)\microsoft visual studio 10.0\vc\include\xfunctional 125

Error 12 error C2784: 'bool std::operator <(const std::unique_ptr<_Ty,_Dx> &,const std::unique_ptr<_Ty2,_Dx2> &)' : could not deduce template argument for 'const std::unique_ptr<_Ty,_Dx> &' from 'const Pair' c:\program files (x86)\microsoft visual studio 10.0\vc\include\xfunctional 125

Error 13 error C2784: 'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'const Pair' c:\program files (x86)\microsoft visual studio 10.0\vc\include\xfunctional 125

Error 14 error C2784: 'bool std::operator <(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'const Pair' c:\program files (x86)\microsoft visual studio 10.0\vc\include\xfunctional 125

Error 15 error C2784: 'bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'const Pair' c:\program files (x86)\microsoft visual studio 10.0\vc\include\xfunctional 125

Error 16 error C2676: binary '<' : 'const Pair' does not define this operator or a conversion to a type acceptable to the predefined operator c:\program files (x86)\microsoft visual studio 10.0\vc\include\xfunctional 125

here is my code for map2.h

#ifndef MAP_H_2
#define MAP_H_2
#include <list>
#include <set>
#include <utility>
#include <iterator>
#include <iostream>
#include <string>
using namespace std;

//pair class header
template<typename F, typename S>
class Pair
{
public:
    Pair(const F& a, const S& b);
    Pair();
    F get_first() const;
    S get_second() const;
private:
    F first;
    S second;
};
//pair class definitions
template<typename F, typename S>
inline Pair<F, S>::Pair(const F& a, const S& b):first(a),second(b){}

template<typename F, typename S>
inline Pair<F, S>::Pair()
{
}

template<typename F, typename S>
inline F Pair<F, S>::get_first() const
{
    return first;
}

template<typename F, typename S>
inline S Pair<F, S>::get_second() const
{
    return second;
}
//map header
class map2
{
public:
    map2();
    void at_put(string key, int value);
    Pair<string, int> at(string key);
    bool contain_key(string key);
    int value_of(string key);
    void remove_key(string key);
    void print();

private:
    set<Pair<string, int>> theList;
};

//map definition
map2::map2(){}

void map2::at_put(string key, int value)
{
    bool notThere = true;
    if(contain_key(key))
    {
        notThere = false;
    }
    if(notThere)
    {
        Pair<string, int> thePair(key, value);
        theList.insert(thePair);
    }
}

Pair<string, int> map2::at(string key)
{
    set<Pair<string, int>>::iterator iter = theList.begin();
    Pair<string, int> thePair;
    string temp;
    for(int x = 0; x<theList.size() ; x++)
    {
        thePair = *iter;
        temp = thePair.get_first();
        if(!key.compare(temp))
        {
            return thePair;
        }
        iter++;
    }
    Pair<string, int> noPair = Pair<string, int>("none", -1);
    return noPair;
}

bool map2::contain_key(string key)
{
    set<Pair<string, int>>::iterator iter = theList.begin();
    Pair<string, int> thePair;
    string temp;
    for(int x = 0; x<theList.size() ; x++)
    {
        thePair = *iter;
        temp = thePair.get_first();
        if(!key.compare(temp))
        {
            return true;
        }
        iter++;
    }
    return false;
}

int map2::value_of(string key)
{
    set<Pair<string, int>>::iterator iter = theList.begin();
    Pair<string, int> thePair;
    string temp;
    for(int x = 0; x<theList.size() ; x++)
    {
        thePair = *iter;
        temp = thePair.get_first();
        if(!key.compare(temp))
        {
            return thePair.get_second();
        }
        iter++;
    }
    return NULL;
}

void map2::remove_key(string key)
{
    set<Pair<string, int>>::iterator iter = theList.begin();
    Pair<string, int> thePair;
    string temp;
    for(int x = 0; x<theList.size() ; x++)
    {
        thePair = *iter;
        temp = thePair.get_first();
        if(!key.compare(temp))
        {
            theList.erase(iter);
        }
        iter++;
    }
}

void map2::print()
{
    set<Pair<string, int>>::iterator iter = theList.begin();
    Pair<string, int> thePair;
    string temp;
    int temp2;
    for(int x = 0; x<theList.size() ; x++)
    {
        thePair = *iter;
        temp = thePair.get_first();
        temp2 = thePair.get_second();
        cout << "Key:" << temp << " Value:" << temp2 << "\n";
        iter++;
    }
}

#endif

and here is my code that has the main function

#include "map2.h"
#include <string>
#include <iostream>

using namespace std;


int main()
{
    map2 theMap;
    theMap.at_put("John", 1000);
    theMap.at_put("Chris", 1000);
    theMap.at_put("John", 1500);
    theMap.at_put("Bob", 1250);

    theMap.print();

    theMap.remove_key("bob");

    theMap.print();

    string findKey;
    cout << "please enter a key to remove" << "\n";
    cin >> findKey;

    bool keyFound = theMap.contain_key(findKey);

    if(keyFound)
    {
        cout << "We found it! The value is:" << theMap.value_of(findKey) << "\n";
    }
    else
    {
        cout << "we don’t have this key " << findKey << "in the map" << "\n";
    }


}
1

1 Answers

1
votes

The problem is this:

error C2676: binary < : const Pair does not define this operator or a conversion to a type acceptable to the predefined operator

There is no < operator defined for the Pair class of map2.h. The C++ STL set class is implemented using a balanced search tree such as a Red-black tree. All elements in the set are stored in a binary tree. The set checks whether it contains an element by comparing an element to the root of the tree, and then recursing into only the one appropriate subtree if there is no match. This requires a comparison operator for the set elements, in order to determine which side of the tree contains the sought-for element.

This requirement is implicitly buried in the documentation for set.

Unfortunately, C++ STL compiler messages are usually awful unhelpful things like this. Over time you’ll get used to it. Some things you can try to make sense of them:

  • Consider only error messages that contain a file you wrote, and ignore error messages about files in the standard library. That wouldn’t have helped in this particular case, but sometimes it will be helpful.
  • Focus on the first or last error message and try to make sense of that
  • Try compiling it with a different compiler like GCC or Clang to see if the error messages are somewhat more helpful.