Hey guys need some help with this.
I m kinda new to c++. I m trying to insert a structure into a set. and when i compile this i m a gettin an error. i guess i need to add a comparator function please let me how do i do that.

Thanks.

#include <iostream>
#include <set>
#include <algorithm>

typedef struct _CONFIG_
{
        char app[10];
        char key[10];
}config_line;


/*struct classcomp {
  bool operator() (const char *lhs, const char *rhs) const
  {
        int rc = strcmp(lhs,rhs);
        if(rc>0)
                return FALSE;
        else if(rc<0)
                return TRUE;
        else
                return FALSE;

  };*/
int main()
{
        config_line data;
        std::set<config_line> nbabudir_set;

        strcpy(data.app,"app1");
        strcpy(data.key,"key1");

        nbabudir_set.insert(data);

}

Error thrown my gcc is
/usr/sfw/lib/gcc/sparc-sun-solaris2.10/3.4.3/../../../../include/c++/3.4.3/bits/stl_function.h: In member function `bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = config_line]':
/usr/sfw/lib/gcc/sparc-sun-solaris2.10/3.4.3/../../../../include/c++/3.4.3/bits/stl_tree.h:869: instantiated from `std::pair<typename std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::insert_unique(const _Val&) [with _Key = config_line, _Val = config_line, _KeyOfValue = std::_Identity<config_line>, _Compare = std::less<config_line>, _Alloc = std::allocator<config_line>]'
/usr/sfw/lib/gcc/sparc-sun-solaris2.10/3.4.3/../../../../include/c++/3.4.3/bits/stl_set.h:314: instantiated from `std::pair<typename std::_Rb_tree<_Key, _Key, std::_Identity<_Key>, _Compare, _Alloc>::const_iterator, bool> std::set<_Key, _Compare, _Alloc>::insert(const _Key&) [with _Key = config_line, _Compare = std::less<config_line>, _Alloc = std::allocator<config_line>]'
set.cpp:32: instantiated from here
/usr/sfw/lib/gcc/sparc-sun-solaris2.10/3.4.3/../../../../include/c++/3.4.3/bits/stl_function.h:227: error: no match for 'operator<' in '__x < __y'

You can pass a comparator to the set as a constructor argument, but it is just as easy to overload operator< for the struct:

#include <cstring>

typedef struct CONFIG_
{
    char app[10];
    char key[10];

    friend bool operator<(CONFIG_ const& a, CONFIG_ const& b)
    {
        return std::strcmp(a.key, b.key) < 0;
    }
} config_line;

Note that I changed _CONFIG_ to CONFIG_ . Names with a leading underscore followed by any uppercase letter are always reserved by the compiler, and names in the global namespace with a leading underscore are also reserved. _CONFIG_ is the kind of name I would expect a compiler to use for something, so it is probably dangerous to use.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.