Quantcast
Channel: CodeGuru Forums - Visual C++ Programming
Viewing all articles
Browse latest Browse all 3042

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

$
0
0
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:-

Quote:

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 < ?

Viewing all articles
Browse latest Browse all 3042

Trending Articles