Set with Sort routine

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



I'm trying to create a set with my sort criteria specified, using MS
Developer Studio 7 and I can't get the syntax correct. Everything I try
creates a compile time error. Here's the basics:

#include <set>

using namespace std;

typedef struct {
int value;
} MyElem;

bool MyCompare( const MyElem& elem1, const MyElem& elem2 )
{
return( elem1.value < elem2.value );
}

void main()
{
set<MyElem, MyCompare> database;
}

The last line compiles with an error:

misc2.cpp(30) : error C2923: 'std::set' : 'MyCompare' is invalid as template
argument '#2', type expected
misc2.cpp(23) : see declaration of 'MyCompare'

Any clues? I've also tried:

set<MyElem> database( MyCompare );

but then I get this error:

misc2.cpp(31) : error C2664: 'std::set<_Kty>::set(const
std::set<_Kty>::key_compare &)' : cannot convert parameter 1 from 'bool
(const MyElem &,const MyElem &)' to 'const std::set<_Kty>::key_compare &'
with
[
_Kty=MyElem
]
and
[
_Kty=MyElem
]
Reason: cannot convert from 'overloaded-function' to 'const
std::set<_Kty>::key_compare'
with
[
_Kty=MyElem
]
No constructor could take the source type, or constructor overload
resolution was ambiguous

Anyone see what I'm doing wrong?

Bruce.


.