c++ STL algorithm with my own classes

This is a discussion on c++ STL algorithm with my own classes within the C++ Programming forums, part of the General Programming Boards category; I've been having some trouble trying to use the STL algorithms on a set of my own struct, I've never ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    266

    c++ STL algorithm with my own classes

    I've been having some trouble trying to use the STL algorithms on a set of my own struct, I've never had a problem using STL with primitive types ... do I need to overload comparison operators for this to work?

    heres some example code which doesnt compile for me!

    Code:
    #include <algorithm>
    #include <vector>
    #include <set>
    #include <map>
    
    using namespace std;
    
    struct choco_size
    {
       int r;
       int c;
    };
    
    bool choco_solve(choco_size size,vector<int>& wanted, map<vector<int>, set<choco_size> >& memo)
    {
     return memo[wanted].find(size) != memo[wanted].end();
    }
    
    int main() {}
    error :
    Code:
    In file included from /usr/include/c++/4.4/bits/stl_tree.h:64,
                     from /usr/include/c++/4.4/set:60,
                     from cpf.cpp:3:
    /usr/include/c++/4.4/bits/stl_function.h: In member function ‘bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = choco_size]’:
    /usr/include/c++/4.4/bits/stl_tree.h:1424:   instantiated from ‘std::_Rb_tree_iterator<_Val> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::find(const _Key&) [with _Key = choco_size, _Val = choco_size, _KeyOfValue = std::_Identity<choco_size>, _Compare = std::less<choco_size>, _Alloc = std::allocator<choco_size>]’
    /usr/include/c++/4.4/bits/stl_set.h:548:   instantiated from ‘typename std::_Rb_tree<_Key, _Key, std::_Identity<_Key>, _Compare, typename _Alloc::rebind<_Key>::other>::const_iterator std::set<_Key, _Compare, _Alloc>::find(const _Key&) [with _Key = choco_size, _Compare = std::less<choco_size>, _Alloc = std::allocator<choco_size>]’
    cpf.cpp:16:   instantiated from here
    /usr/include/c++/4.4/bits/stl_function.h:230: error: no match for ‘operator<’ in ‘__x < __y’
    THANKS!!!

    any ideas?
    Last edited by rodrigorules; 10-26-2010 at 06:13 PM.

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    set<T> requires that T be less-than comparable, so yes, you either need to implement operator<() for the class, or use a comparison functor.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. College Classes
    By nubby in forum C Programming
    Replies: 2
    Last Post: 10-07-2007, 12:32 AM
  2. inherited classes and stl container
    By rahulsk1947 in forum C++ Programming
    Replies: 9
    Last Post: 05-05-2007, 02:27 PM
  3. Questions on Classes
    By Weng in forum C++ Programming
    Replies: 2
    Last Post: 11-18-2003, 05:49 AM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21