CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 17

Threaded View

  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    std::set and struct - can they work together?

    This works...
    Code:
    std::set<int> IntSet;
    
    int hello = 6;
    IntSet.insert (hello);
    but this doesn't work...
    Code:
    typedef struct {
              void *p;
              unsigned int n;
            } my_struct;
    
    std::set<my_struct> StructSet;
    
    my_struct goodbye;
    goodbye.p = whatever();
    goodbye.n = 1;
    
    StructSet.insert (goodbye);
    If I try to use structs in a std::set I get this error from MSVC:-

    C:\Program Files\Microsoft Visual Studio 8\VC\include\functional(143) : 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 my_struct'

    C:\Program Files\Microsoft Visual Studio 8\VC\include\xtree(1372) : see declaration of 'std:: operator <'

    C:\Program Files\Microsoft Visual Studio 8\VC\include\functional(142) : while compiling class template member function 'bool std::less<_Ty>:: operator ()(const _Ty &,const _Ty &) const'

    with

    [

    _Ty=my_struct

    ]
    std::set is using std:: operator < to determine if the struct is already present in the set - but std:: operator < doesn't seem to work for a struct (presumably, it has no way of knowing how big the struct is). I need to keep std::set for compatibility with something else, so what's the best way to solve this problem? Can I tell std::set to use my own defined comparison operator, instead of std:: operator < ?
    Last edited by John E; July 9th, 2013 at 01:43 AM.
    "A problem well stated is a problem half solved.” - Charles F. Kettering

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured