Is it possible to check if custom class exists (key) in map?

I'm trying to see if my map already contains a class of "Bar' in it's keys:

1
2
3
4
5
6
7
8
9
10
//Foo.h
map <Bar, int> inventory;

//Foo.cpp
bool Foo::CheckIfItemExists(Inventory& _inventory, Bar bar)
{
   map<Bar, int>::iterator it = _inventory.inventory.find(bar);
   if (it != inventory.inventory.end()){return true;}
   return false;
}


I'm getting: binary '<': no operator found which takes a left-hand operand of type 'const Bar' (or there is no acceptable conversion).
Last edited on
map already contains a class of "Bar' in it's keys:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# include <iostream>
# include <map>
# include <type_traits>
# include <iomanip>

struct Bar{};

int main()
{
   std::map<Bar, int>::key_type key;

   std::cout << std::boolalpha << std::is_same<decltype(key), int> :: value << "\n"
                            << std::is_same<decltype(key), Bar> :: value << "\n";
}


Last edited on
> I'm getting: binary '<': no operator found which takes a left-hand operand of type 'const Bar'
> (or there is no acceptable conversion).

The const-qualified key_type const Bar in std::map< Bar, int > must be LessThanComparable

To satisfy this requirement, types that do not have built-in comparison operators have to provide a user-defined (const-correct) operator<
http://en.cppreference.com/w/cpp/concept/LessThanComparable
Topic archived. No new replies allowed.